-
Feb 25th, 2012, 01:57 PM
#1
Thread Starter
Lively Member
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
-
Feb 25th, 2012, 03:04 PM
#2
Re: Copy file from resources
Originally Posted by TeachMeVB
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")
Last edited by kareninstructor; Jul 18th, 2012 at 04:43 PM.
-
Feb 25th, 2012, 03:08 PM
#3
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.
-
Feb 25th, 2012, 03:09 PM
#4
Re: Copy file from resources
What if you try;
vb Code:
My.Computer.FileSystem.WriteAllText("C:\windows\media\Startup.wav",My.Resources.Startup,True)
Good Luck!
-
Nevermind, I suggest the two posters above.
vb Code:
System.IO.File.WriteAllBytes("C:\windows\media\Startup.wav", My.Resources.Startup)
Last edited by proneal; Feb 25th, 2012 at 03:13 PM.
-
Feb 25th, 2012, 03:10 PM
#5
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:
IO.File.WriteAllBytes(filePath, My.Resources.SomeResource)
-
Feb 25th, 2012, 03:10 PM
#6
Re: Copy file from resources
Originally Posted by proneal
What if you try;
vb Code:
My.Computer.FileSystem.WriteAllText("C:\windows\media\Startup.wav",My.Resources.Startup,True)
Good Luck!
As the name suggests, WriteAllText is for writing text.
-
Feb 25th, 2012, 03:11 PM
#7
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.
-
Feb 25th, 2012, 03:33 PM
#8
Thread Starter
Lively Member
Re: Copy file from resources
Originally Posted by minitech
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?
-
Feb 25th, 2012, 03:37 PM
#9
Thread Starter
Lively Member
Re: Copy file from resources
Originally Posted by jmcilhinney
As the name suggests, WriteAllText is for writing text.
This doesn't work
-
Feb 25th, 2012, 03:38 PM
#10
Re: Copy file from resources
Originally Posted by TeachMeVB
"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.
-
Feb 25th, 2012, 03:51 PM
#11
Re: Copy file from resources
Originally Posted by TeachMeVB
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.
-
Feb 25th, 2012, 05:58 PM
#12
Re: Copy file from resources
Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|