.NET: open files embedded in a resource file

How can I open files embedded in a resource file, like a file on a hard drive (with an absolute path)?

+6
c #
source share
1 answer

Suppose you have a test.xml file built into the assembly. You can use the GetManifestResourceStream method to get a stream pointing to the contents:

 class Program { static void Main() { var assembly = Assembly.GetExecutingAssembly(); using (var stream = assembly.GetManifestResourceStream("ProjectName.test.xml")) using (var reader = new StreamReader(stream)) { Console.WriteLine(reader.ReadToEnd()); } } } 

Thus, the contents of the file are read into memory. You can also save it to your hard drive and then access it in an absolute path, but this may not be necessary since you already have the contents of the file.

+11
source share

All Articles