none
Read a Binary Resource file into a Byte Array RRS feed

  • Question

  • Hi All,

    I have a file in my applications Resources that I need to read into a byte array. The file is 8K (8192 bytes) of random binary data.

    The filename is RndBin and its type is set to Binary in the properties window. The byte array name is Bin() and needs to be declared as Public.

    I have tried the following as described in another post, but it does not work. 'Properties' and 'Resources' doesn't even appear as an option. Tried a variety of code, but no luck.

    If you click on the file in resources and view the properties window, you could set the File Type to binary. Then you can access the bytearry in code with simple:
    Dim byteArray = Properties.Resources.FileName

    Public RndBin(8191) as Byte = Properties.Resources.RndBin   ' (Does not work)

    Any example or assistance is greatly appreciated.

    DW

    Friday, November 17, 2017 6:38 PM

Answers

  •  An mp3 file and files like MS Excel or Word documents are already accessible from the resources as a Byte Array type without using a MemoryStream.  Rtf and Txt files are accessible as String types,  and Images as Bitmap types.  Wave (.wav) audio files are usually accessible as an UnmanagedMemoryStream.  8)

     This is why i am wondering how the file was added to the resources and what type of file it is besides 'a binary file'.  8)

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim bts() As Byte = My.Resources.Saturday_Night_Special 'mp3 file
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, November 17, 2017 9:39 PM
    • Marked as answer by dw80916 Friday, November 17, 2017 11:03 PM
    Friday, November 17, 2017 9:37 PM
  • Thanks, Frank

    Decided not to use the RTF files as a resource, as they were way too big. They were application documentation files, and will end up on a separate web site. Project has gone through many revisions to keep the Store submission & installation as simple as possible, and to comply with BIS regulations.

    There are better ways, but that aside -- what I wanted to show you is that your resources aren't as safe as what you might think.

    From earlier, I put this RTF file in:

    When I disassemble (obviously no obfuscation here), well -- have a look:

    http://www.fls-online.net/VBNet_Forum/11-17-17/ScreenShot11.png

    I can get ALL of them. Figuring out the type (I have an assembly that I put together a while back to help do that), then I can change the file extension and ... I have everything you put in your resources. Images, song files, videos ...

    It's not hard to do!

    *****

    Anyway, I'm glad you got it all worked out. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by dw80916 Friday, November 17, 2017 11:04 PM
    Friday, November 17, 2017 10:35 PM

All replies

  • Hi All,

    I have a file in my applications Resources that I need to read into a byte array. The file is 8K (8192 bytes) of random binary data.

    The filename is RndBin and its type is set to Binary in the properties window. The byte array name is Bin() and needs to be declared as Public.

    I have tried the following as described in another post, but it does not work. 'Properties' and 'Resources' doesn't even appear as an option. Tried a variety of code, but no luck.

    If you click on the file in resources and view the properties window, you could set the File Type to binary. Then you can access the bytearry in code with simple:
    Dim byteArray = Properties.Resources.FileName

    Public RndBin(8191) as Byte = Properties.Resources.RndBin   ' (Does not work)

    Any example or assistance is greatly appreciated.

    DW

    DW,

    This looks to be right (I haven't tried it though):

    https://stackoverflow.com/questions/10412401/how-to-read-an-embedded-resource-as-array-of-bytes-without-writing-it-to-disk

    If the C# bothers you, don't let it:

    Public Shared Function ExtractResource(filename As [String]) As Byte()
    	Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
    	Using resFilestream As Stream = a.GetManifestResourceStream(filename)
    		If resFilestream Is Nothing Then
    			Return Nothing
    		End If
    		Dim ba As Byte() = New Byte(resFilestream.Length - 1) {}
    		resFilestream.Read(ba, 0, ba.Length)
    		Return ba
    	End Using
    End Function
    
    '=======================================================
    'Service provided by Telerik (www.telerik.com)
    'Conversion powered by NRefactory.
    'Twitter: @telerik
    'Facebook: facebook.com/telerik
    '=======================================================

    I doubt that you need the function to be public or shared but that's your call.

    Try that and let me know if it works for you please?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 17, 2017 7:10 PM
  • What does "Does not work" mean? Does Intellisense show that as a valid name? Or does it get a compile error? Probably Intellisense does not show it as valid. Use Intellisense to determine the correct name.



    Sam Hobbs
    SimpleSamples.Info

    Friday, November 17, 2017 7:11 PM
  •  Did you follow the steps in the link below to add the resource to your project?  If you did,  then it should be available by typing the words 'My.Resources.NameOfResource'.  What was the extension of the file you added and how did you create the file,  if you did?  Not all file types will be added to your resources as a Byte Array type.

    How to: Add or Remove Resources


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, November 17, 2017 9:07 PM
    Friday, November 17, 2017 8:47 PM
  • I've put the function into my project.

    Have a public declaration: Public Bin() As Byte

    Call the Function: Bin = ExtractResource("RndBin")

    No exceptions are thrown, but the Bin() array is still null.

    Obviously there's something I'm still doing wrong. 

    Friday, November 17, 2017 9:05 PM
  • I've put the function into my project.

    Have a public declaration: Public Bin() As Byte

    Call the Function: Bin = ExtractResource("RndBin")

    No exceptions are thrown, but the Bin() array is still null.

    Obviously there's something I'm still doing wrong. 

    I don't know who you're talking to but this seems to work.

    I put an .mp3 in the resources (a song by AeroSmith) and to get it as a byte array:

        Imports System.IO
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim ms As New MemoryStream(DirectCast(My.Resources.Crazy, Byte()))
    
            If ms IsNot Nothing Then
                Dim bytes As Byte() = ms.ToArray
    
                Stop
            End If
    
        End Sub
    End Class

    The variable "bytes" has the data now.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 17, 2017 9:23 PM
  • I've put the function into my project.

    Have a public declaration: Public Bin() As Byte

    Call the Function: Bin = ExtractResource("RndBin")

    No exceptions are thrown, but the Bin() array is still null.

    Obviously there's something I'm still doing wrong. 

    DW,

    Are you still wrangling with RTF files like you were a few months back?

    The resource manager doesn't see that as binary data.

    If I'm right, try this please:

    Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(My.Resources.VBForum1)
    
    Stop

    "VBForum1" is the name of the RTF file I put in the resources. Obviously you want to use the one you put in yours, but you get the idea.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 17, 2017 9:30 PM
  •  An mp3 file and files like MS Excel or Word documents are already accessible from the resources as a Byte Array type without using a MemoryStream.  Rtf and Txt files are accessible as String types,  and Images as Bitmap types.  Wave (.wav) audio files are usually accessible as an UnmanagedMemoryStream.  8)

     This is why i am wondering how the file was added to the resources and what type of file it is besides 'a binary file'.  8)

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim bts() As Byte = My.Resources.Saturday_Night_Special 'mp3 file
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, November 17, 2017 9:39 PM
    • Marked as answer by dw80916 Friday, November 17, 2017 11:03 PM
    Friday, November 17, 2017 9:37 PM
  •  An mp3 file and files like MS Excel or Word documents are already accessible from the resources as a Byte Array type without using a MemoryStream.  Rtf and Txt files are accessible as String types,  and Images as Bitmap types.  Wave (.wav) audio files are usually accessible as an UnmanagedMemoryStream.  8)

     This is why i am wondering how the file was added to the resources and what type of file it is besides 'a binary file'.  8)

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim bts() As Byte = My.Resources.Saturday_Night_Special 'mp3 file
        End Sub
    End Class


    If you say it can`t be done then i`ll try it


    Who are you addressing?

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 17, 2017 9:43 PM
  •  An mp3 file and files like MS Excel or Word documents are already accessible from the resources as a Byte Array type without using a MemoryStream.  Rtf and Txt files are accessible as String types,  and Images as Bitmap types.  Wave (.wav) audio files are usually accessible as an UnmanagedMemoryStream.  8)

     This is why i am wondering how the file was added to the resources and what type of file it is besides 'a binary file'.  8)

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim bts() As Byte = My.Resources.Saturday_Night_Special 'mp3 file
        End Sub
    End Class


    If you say it can`t be done then i`ll try it


    Who are you addressing?

    "A problem well stated is a problem half solved.” - Charles F. Kettering

     Whoever will listen to me ramble on i guess.  LOL

     I replied to you but,  forgot to add the @Frank after typing.  I figured it may be helpful info to dw80916 too though?  8)


    If you say it can`t be done then i`ll try it

    Friday, November 17, 2017 9:50 PM
  • This is why i am wondering how the file was added to the resources and what type of file it is besides 'a binary file'.

    If he were to serialize data to a binary file, could that file be added to the resources?

    Answer: Yes

    If he were to zip the file up, could that file be added to the resources?

    Answer: Yes

    Would it be a binary file?

    Answer: Yes

    Can you read it without a Stream object?

    Answer: Yes but what's the next step? It will likely involve a stream somewhere along the lines (a filestream if it's being written to file).


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 17, 2017 9:51 PM
  • The file was created by another utility I made in VS 2017. The file content is completely random byte values. I gave the file extension as .bin 

    The file is certainly in the project resources and in the project resource directory. Anyway, just realized I've been suffering a massive brain fart here and forgetting to use the 'My'. Makes a big difference. When you go 7 years averaging 3 hrs sleep each day, this stuff happens.

    Thanks to Charles F. Kettering for the code sample and IronRazerz for the bold text  'My.Resources.NameOfResource'

    I'll continue and see what happens.

    Friday, November 17, 2017 9:51 PM
  • The file was created by another utility I made in VS 2017. The file content is completely random byte values. I gave the file extension as .bin 

    The file is certainly in the project resources and in the project resource directory. Anyway, just realized I've been suffering a massive brain fart here and forgetting to use the 'My'. Makes a big difference. When you go 7 years averaging 3 hrs sleep each day, this stuff happens.

    Thanks to Charles F. Kettering for the code sample and IronRazerz for the bold text  'My.Resources.NameOfResource'

    I'll continue and see what happens.

    Frank ... not Charles. ;-)

    *****

    Wait ... before you do that, let's talk some.

    Is the file you have embedded in the resources proprietary or are you just using the resources to carry it along?

    I'll explain why I'm asking when you answer.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 17, 2017 9:55 PM
  • Thanks to Charles F. Kettering for the code sample and IronRazerz for the bold text  'My.Resources.NameOfResource'

    Also, I said "Use Intellisense to determine the correct name." and other things.


    Sam Hobbs
    SimpleSamples.Info

    Friday, November 17, 2017 9:58 PM
  • The file was created by another utility I made in VS 2017. The file content is completely random byte values. I gave the file extension as .bin 

    The file is certainly in the project resources and in the project resource directory. Anyway, just realized I've been suffering a massive brain fart here and forgetting to use the 'My'. Makes a big difference. When you go 7 years averaging 3 hrs sleep each day, this stuff happens.

    Thanks to Charles F. Kettering for the code sample and IronRazerz for the bold text  'My.Resources.NameOfResource'

    I'll continue and see what happens.

     If it is just random byte values saved to a file,  then it should be accessible as i mentioned.  Using the first code below,  i saved some 'random' bytes to a file...

            Dim svbts() As Byte = {66, 22, 128, 255, 0, 84}
            IO.File.WriteAllBytes("C:\TestFolder\MyBinaryFile.bin", svbts)
    
     

     I then added the file as a resources in a testing application and used the below code to get a byte array.  Saving it back to a file is optional...

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim bts() As Byte = My.Resources.MyBinaryFile
            'IO.File.WriteAllBytes("C:\TestFolder\ExtractedBinaryFile.bin", bts) 'if you want to save it to the hard drive
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    Friday, November 17, 2017 10:10 PM
  • Thanks, Frank

    Decided not to use the RTF files as a resource, as they were way too big. They were application documentation files, and will end up on a separate web site. Project has gone through many revisions to keep the Store submission & installation as simple as possible, and to comply with BIS regulations.

    Friday, November 17, 2017 10:24 PM
  • It's a file I created and in resources just to carry it along. Random data, but I need that particular random data, and not accessible to the user. Putting in resources because I don't want the hassle of having to use an installer just to put one file in the Program Data folder.
    Friday, November 17, 2017 10:33 PM
  • Thanks, Frank

    Decided not to use the RTF files as a resource, as they were way too big. They were application documentation files, and will end up on a separate web site. Project has gone through many revisions to keep the Store submission & installation as simple as possible, and to comply with BIS regulations.

    There are better ways, but that aside -- what I wanted to show you is that your resources aren't as safe as what you might think.

    From earlier, I put this RTF file in:

    When I disassemble (obviously no obfuscation here), well -- have a look:

    http://www.fls-online.net/VBNet_Forum/11-17-17/ScreenShot11.png

    I can get ALL of them. Figuring out the type (I have an assembly that I put together a while back to help do that), then I can change the file extension and ... I have everything you put in your resources. Images, song files, videos ...

    It's not hard to do!

    *****

    Anyway, I'm glad you got it all worked out. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by dw80916 Friday, November 17, 2017 11:04 PM
    Friday, November 17, 2017 10:35 PM