0

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.

Paul Alexander
  • 2,686
  • 4
  • 33
  • 69

2 Answers2

1

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.

Fabulous
  • 2,393
  • 2
  • 20
  • 27
0

I achieved this by using File.WriteAllBytes()

Example:

File.WriteAllBytes("C:\\MyApp\\TextExample.json", MyApp.Properties.Resources.TextExample);
Paul Alexander
  • 2,686
  • 4
  • 33
  • 69