I’ve made a new proyect on Visual Studio 2010. It’s a Windows Forms one, and done with .NET Framework 4 Client Profile.
I’ve put on it a embeded WAV sound file.
The only thing that I want to is to copy it to some path, let’s say desktop as example. How can I achieve this?
I’ve already tried these:
How to copy file From Resources?
Read a file from a resource and write it to disk in C#
Write file from assembly resource stream to disk
However, I’m always facing troubles. For example:
If I add a audio resource that’s named Take Over Control.wav, it’s added with the name of Take_Over_Control. Then, I just add a button to my main form that says “Copy”. Then, the click event of that button has this:
private void ButtonCopyClick(object sender, EventArgs e)
{
Stream resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("Take_Over_Control");
if (resource == null)
{
throw new ArgumentException();
}
Stream output = File.OpenWrite(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)+"\\Sound.wav");
resource.CopyTo(output);
}
The thing is… with no matter if I put Take_Over_Control or Resources.Take_Over_Control or Properties.Resources.Take_Over_Control, etc, in the GetManifestResourceStream part, I’m always stuck on throw new ArgumentException();, so, it means that resource is null, and it isn’t well-asigned on Stream resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("Take_Over_Control"); line.
And there are troubles with others methods that I’ve tried. What’s wrong in here? How to copy this embeded audio file by the right way!?
Seeking into the Resources.Designer.cs file, I saw a ResourceManager.GetStream… so, I use this now on my click event:
And it just does the right job of copy the WAV file to the desktop. And also… now I’m free of reflection!
Important to notice that when WAV files are added to a Visual Studio 2010 C# .NET Framework 4 Client Profile project, it isn’t saved as
byte[], it’s saved asUnmanagedMemoryStream. Because of this, the need of convert it tobyte[]or just put it in aStreamviaResourceManager, and,ResourceManagerway is shorter!