How can I extract a file from an embedded resource and save it to disk?
I have found that the easiest way to do this is to use Properties.Resources
and File
. Here is the code I use (requires using System.IO
)...
For Binary files:
File.WriteAllBytes(fileName, Properties.Resources.file);
For Text files:
File.WriteAllText(fileName, Properties.Resources.file);
I'd suggest doing it easier. I assume that the resource exists and the file is writable (this might be an issue if we're speaking about system directories).
public void WriteResourceToFile(string resourceName, string fileName)
{
using(var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
using(var file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
resource.CopyTo(file);
}
}
}