Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET: Open files, which are embedded in a resource file

Tags:

c#

.net

How can I open files, which are embedded in a resource file, like a file on the harddisk (with an absolute path) ?

like image 696
Kottan Avatar asked Oct 20 '10 08:10

Kottan


1 Answers

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.

like image 163
Darin Dimitrov Avatar answered Sep 29 '22 01:09

Darin Dimitrov