Paul Alexander
Paul Alexander

Reputation: 2748

c#/WinForms - Take file from resources and place it in directory on C: drive

I'm sure this has been done before and is probably quite simple to achieve but I can't seem to find anything on the internet when I've searched.

I'm looking for a way to pickup/copy a file from my applications resources and place it into a folder on my the C: drive.

Can this be done? Or should I read in the contents of the file and then create a new one in the desired directory?

Any pointers/advice would be appreciated! Thanks.

Upvotes: 0

Views: 211

Answers (2)

Paul Alexander
Paul Alexander

Reputation: 2748

I achieved this by using File.WriteAllBytes()

Example:

File.WriteAllBytes("C:\\MyApp\\TextExample.json", MyApp.Properties.Resources.TextExample);

Upvotes: 0

Fabulous
Fabulous

Reputation: 2423

You can take the contents of the embedded file and write them out to your desired location as follows:

using (var fs = new FileStream(@"C:\Temp\Foo.txt", FileMode.Create))
using (var sw = new StreamWriter(fs))
{
    var data = Stuff.Foo;
    sw.Write(data);
}

I did this for a text file embedded in Stuff.resx in my project. There's a Write overload for various types so you can use what you need. For instance, an image will come back as a bitmap.

Upvotes: 1

Related Questions