开发者

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

开发者 https://www.devze.com 2023-01-20 18:41 出处:网络
How can I open files, which are embedded in a resource file, 开发者_如何学运维like a file on the harddisk (with an absolute path) ?Suppose that you have the test.xml file embedded into the assembly. Y

How can I open files, which are embedded in a resource file, 开发者_如何学运维like a file on the harddisk (with an absolute path) ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号