读取嵌入式文本文件 [英] Reading an embedded text file

查看:64
本文介绍了读取嵌入式文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个完整的项目,该项目运行完美。我的问题与安装项目有关。当我在另一台计算机上使用该文本文件时,即使在部署过程中该文本文件位于资源文件夹中也无法找到!

I have created a full project which works perfectly. My problem concerns the setup project. When I use it on another computer, the text file cannot be found even if they are inside the resource folder during the deployment!

如何确保我的程序可以找到将这些文本文件安装到另一台计算机上之后!

How can I ensure that my program will find those text files after installing the software on another computer!

我一直在寻找这种解决方案,但徒劳无功。请帮我解决这个问题。如果我可以得到完整的代码,那我会感到非常高兴!

I have been looking for this solution but in vain. Please help me sort this out. If I can get a full code that does that i will be very happy!

推荐答案

首先设置文本文件的生成操作

FIrst set the build action of the text file to "EmbeddedResource".

然后在您的代码中读取文件:

Then to read the file in your code:

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "AssemblyName.MyFile.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
    using (StreamReader reader = new StreamReader(stream))
    {
        string result = reader.ReadToEnd();
    }
}

如果您不知道嵌入式资源会执行此操作以查找名称,并且应该清楚地知道您的文件是:

If you can't figure out the name of the embedded resource do this to find the names and it should be obvious which your file is:

assembly.GetManifestResourceNames();

这是假设您希望将文本文件嵌入到程序集中。如果没有,那么您可能只想更改安装项目以在安装过程中包括文本文件。

这篇关于读取嵌入式文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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