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

╄→гoц情女王★ 提交于 2021-02-18 00:53:56

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/3976180/net-open-files-which-are-embedded-in-a-resource-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!