.NET:打开文件,这些文件嵌入在资源文件中 [英] .NET: Open files, which are embedded in a resource file

查看:49
本文介绍了.NET:打开文件,这些文件嵌入在资源文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打开嵌入在资源文件中的文件,例如硬盘上的文件(具有绝对路径)?

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

推荐答案

假定您已将 test.xml 文件嵌入到程序集中.您可以使用 GetManifestResourceStream 方法来获取流指向内容:

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.

这篇关于.NET:打开文件,这些文件嵌入在资源文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆