Copy file from resources-VBForums
Results 1 to 12 of 12

Thread: Copy file from resources

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Copy file from resources

    Hey.

    I want to copy my Startup.wav file from the programs resources, but i don't know how to do it.

    I have tried: system.IO.File.Copy(My.resources.Startup, "C:\windows\media\Startup.wav", true)
    But it doesn't work. I've spent some time searching the web, but found no usefull awnsers to my problem. Maybe i'm not searching for the right things...

    Do anyone of you know a way I can do this??

    Thanks

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,322

    Re: Copy file from resources

    Quote Originally Posted by TeachMeVB View Post
    Hey.

    I want to copy my Startup.wav file from the programs resources, but i don't know how to do it.

    I have tried: system.IO.File.Copy(My.resources.Startup, "C:\windows\media\Startup.wav", true)
    But it doesn't work. I've spent some time searching the web, but found no usefull awnsers to my problem. Maybe i'm not searching for the right things...

    Do anyone of you know a way I can do this??

    Thanks
    When you say copy startup.wav from the programs resource this indicates that you have added startup.wav to My.Resources of your program so why are you trying to copy from C:\windows\media ?

    If you have added startup.wav to your program resource via Project properties, Resources tab, Add resource button, add existing file at the top of the Resource tab then simply in the proper place in your code type out My.Resources.startup and note the tool tip indicating the file type (the image below shows an executable file stored as a resource), most likely an array of bytes which will allow you to determine the proper method to save the byte array to disk.

    The following language extension will do this for you.
    Code:
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub FileSave(ByVal sender() As Byte, ByVal FileName As String)
        Dim FileStream As New System.IO.FileStream(FileName, System.IO.FileMode.OpenOrCreate)
        Dim BinaryWriter As New System.IO.BinaryWriter(FileStream)
    
        BinaryWriter.Write(sender)
        BinaryWriter.Close()
        FileStream.Close()
    End Sub
    So for me I would type in
    Code:
    My.Resources.ColorCop.FileSave("SomeName.exe")

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,135

    Re: Copy file from resources

    File.Copy copies a file, but you don't have a file. You have to pass it the path of the file to be copied, but you don't have a path. A resource is data compiled into your EXE. My.Resources returns that data as a particular type depending on the type of the original file. For a WAV file embedded as a resource, it should either be returned as a Byte array or a Stream of some sort (an UnmanagedMemoryStream if I remember correctly).

    If it's a Byte array then you can simply call File.WriteAllBytes to write the data to a file. If it's a Stream then it's only a little more complex. In that case you will have to read the data from the Stream into a Byte array first, then write it out the same way. You should be able to get the Length of the Stream and use that to create a Byte array of the appropriate size. You can then call Read on the Stream to read all the data into the Byte array.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Copy file from resources

    What if you try;

    vb Code:
    1. My.Computer.FileSystem.WriteAllText("C:\windows\media\Startup.wav",My.Resources.Startup,True)

    Good Luck!

    -

    Nevermind, I suggest the two posters above.

    vb Code:
    1. System.IO.File.WriteAllBytes("C:\windows\media\Startup.wav", My.Resources.Startup)
    Last edited by proneal; Feb 25th, 2012 at 03:13 PM.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,135

    Re: Copy file from resources

    I see from KI's post that it will be a Byte array, so the job is simple. KI is a little too keen on extension methods I think. In this case it's a one-liner:
    vb.net Code:
    1. IO.File.WriteAllBytes(filePath, My.Resources.SomeResource)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,135

    Re: Copy file from resources

    Quote Originally Posted by proneal View Post
    What if you try;

    vb Code:
    1. My.Computer.FileSystem.WriteAllText("C:\windows\media\Startup.wav",My.Resources.Startup,True)

    Good Luck!
    As the name suggests, WriteAllText is for writing text.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Copy file from resources

    You can use the System.IO.File.WriteAllBytes method to do this. Assuming System.IO is imported:

    Code:
    File.WriteAllBytes("C:\Windows\Media\Startup.wav", My.Resources.Startup)
    EDIT: It seems I'm a little late to reply with this Oh well.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Re: Copy file from resources

    Quote Originally Posted by minitech View Post
    You can use the System.IO.File.WriteAllBytes method to do this. Assuming System.IO is imported:

    Code:
    File.WriteAllBytes("C:\Windows\Media\Startup.wav", My.Resources.Startup)
    EDIT: It seems I'm a little late to reply with this Oh well.
    "File" = an error: Name File is not declared.
    What do i do?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Re: Copy file from resources

    Quote Originally Posted by jmcilhinney View Post
    As the name suggests, WriteAllText is for writing text.
    This doesn't work

  10. #10
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Copy file from resources

    Quote Originally Posted by TeachMeVB View Post
    "File" = an error: Name File is not declared.
    What do i do?
    As stated in the post, it assumes System.IO is imported. If it's not (you apparently didn't follow that step) then just fully qualify the name as System.IO.File.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,135

    Re: Copy file from resources

    Quote Originally Posted by TeachMeVB View Post
    This doesn't work
    Um, yeah, that's what I said. WriteAllText is for writing text. You're not writing text so it obviously isn't for what you're doing. You've already been shown two different ways to use File.WriteAllBytes and you managed not to use either of them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Copy file from resources

    Quote Originally Posted by jmcilhinney View Post
    As the name suggests, WriteAllText is for writing text.
    I Apologize OP, I meant writeallbytes using File object instead, which is why I edited the post to put the right data in.
    I was not awake yet, and so, now fully embarrassed.


    It's all good though.
    Answer provided
    Last edited by proneal; Feb 25th, 2012 at 05:58 PM. Reason: syntax error: write/right..

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.