Search code examples
vb.netbinarywriter

Send Specific Values to BinaryWriter


i need help here, i did run out of ideas. I need to do in the following code, to be able to chose a file from within the project (My.resources), instead of HardCoding this line FSR.Write(My.Resources._1_5, 0, My.Resources._1_5.Length) i would like to be able to pass a selected value, but i cant store My.Resources._1_5 into a Byte variable, it kept on saying cannot store 1 dimensional array of Bytes into Byte. _1_5 is a doc file, and i have a long list of files i want to be able to chose from.

 Dim TempFileName As String = "TMPDoc.doc"
        Dim TempFolder As String = My.Computer.FileSystem.SpecialDirectories.Temp
        Dim path As String = Application.StartupPath & "\"
        TempFileName = path & TempFileName

        Dim FS As New System.IO.FileStream(TempFileName, IO.FileMode.Create, FileAccess.Write)
        Dim FSR As New System.IO.BinaryWriter(FS)
        FSR.Write(My.Resources._1_5, 0, My.Resources._1_5.Length)
        FSR.Close()
        FS.Close()

        appWord.Documents.Open(TempFileName)

Not sure if i am explaining myself.


Solution

  • If, as is most likely the case, the My.Resources._1_5 property is exposed as a byte array, then you can simply create a byte array variable to point to it, like this:

    Dim resource_1_5() As Byte = My.Resources._1_5
    

    Or you could create a list of byte arrays and add it to the list, like this:

    Dim resources As New List(Of Byte())()
    resources.Add(My.Resources._1_5)
    

    However, what you may really be trying to do is just access the resource by string name rather than via a concrete property. If that's the case, you can get it by name via the ResourceManager, like this:

    Dim resource_1_5() As Byte = DirectCast(My.Resources.ResourceManager.GetObject("_1_5", My.Resources.Culture), Byte())
    

    Note that in the above sample, the GetObject method returns a Byte array, but it casts it as Object. Therefore, you need to use DirectCast or CType to cast it back to a Byte array.