How can I open files, which are embedded in a resource file, like a file on the harddisk (with an absolute path) ?
Asked
Active
Viewed 6,620 times
1 Answers
12
Suppose that you have the test.xml
file embedded into the assembly. You could use the GetManifestResourceStream method to obtain a stream pointing towards 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());
}
}
}
This way the contents of the file is read into memory. You can also store it to the harddisk and then access by absolute path but this might not be necessary as you already have the contents of the file.

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
2You need to include the project's default namespace before the filename, not the project name. – Rich Jan 04 '11 at 18:47
-
1If the file does not exist as an embedded resource, your 'stream' variable will be null. This will cause an exception when creating your StreamReader. – Kilhoffer Jan 30 '12 at 17:37