如何读取嵌入的资源文本文件 [英] How to read embedded resource text file
问题描述
我如何读取使用的StreamReader,并返回一个字符串嵌入的资源(文本文件)?我现在的脚本使用Windows窗体和文本框,它允许用户查找和未嵌入一个文本文件替换文本。
私人无效的button1_Click(对象发件人,EventArgs的)
{
StringCollection strValuesToSearch =新StringCollection();
strValuesToSearch.Add(苹果);
串stringToReplace;
stringToReplace = textBox1.Text;
StreamReader的FileReader对象=新的StreamReader(@C:\ MyFile.txt的);
串FileContents;
FileContents = FileReader.ReadToEnd();
FileReader.Close();
的foreach(字符串s的strValuesToSearch)
{
如果(FileContents.Contains(多个))
FileContents = FileContents.Replace(S,stringToReplace);
}
的StreamWriter的FileWriter =新的StreamWriter(@MyFile.txt的);
FileWriter.Write(FileContents);
FileWriter.Close();
}
您可以使用<一个href="http://msdn.microsoft.com/en-us/library/xc4235zt(v=VS.85).aspx"><$c$c>Assembly.GetManifestResourceStream$c$c>方法:
-
添加使用以下
使用的System.Reflection;
-
设置相关文件的属性:
以价值嵌入的资源参数生成操作 -
使用下面的code
VAR组装= Assembly.GetExecutingAssembly();
VAR资源名称=MyCompany.MyProduct.MyFile.txt;
使用(流流= assembly.GetManifestResourceStream(资源名称))
使用(StreamReader的读者=新的StreamReader(流))
{
字符串结果= reader.ReadToEnd();
}
资源名称
是一个内嵌于组装
的资源之一的名称。
例如,如果您嵌入被放置在默认的命名空间项目的根目录下名为
,然后MyFile.txt的
文本文件MyCompany.MyProduct 资源名称
是MyCompany.MyProduct.MyFile.txt
。
您可以通过获取所有资源的列表在装配的<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getmanifestresourcenames%28v=vs.85%29.aspx"><$c$c>Assembly.GetManifestResourceNames$c$c>方法。
How do I read an embedded resource (text file) using StreamReader and return it as a string? My current script uses a Windows form and textbox that allows the user to find and replace text in a text file that is not embedded.
private void button1_Click(object sender, EventArgs e)
{
StringCollection strValuesToSearch = new StringCollection();
strValuesToSearch.Add("Apple");
string stringToReplace;
stringToReplace = textBox1.Text;
StreamReader FileReader = new StreamReader(@"C:\MyFile.txt");
string FileContents;
FileContents = FileReader.ReadToEnd();
FileReader.Close();
foreach (string s in strValuesToSearch)
{
if (FileContents.Contains(s))
FileContents = FileContents.Replace(s, stringToReplace);
}
StreamWriter FileWriter = new StreamWriter(@"MyFile.txt");
FileWriter.Write(FileContents);
FileWriter.Close();
}
You can use the Assembly.GetManifestResourceStream
Method:
Add the following using
using System.Reflection;
Set property of relevant file:
Parameter Build Action with value Embedded ResourceUse the following code
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyCompany.MyProduct.MyFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
}
resourceName
is the name of one of the resources embedded in assembly
.
For example, if you embed a text file named "MyFile.txt"
that is placed in the root of a project with default namespace "MyCompany.MyProduct"
, then resourceName
is "MyCompany.MyProduct.MyFile.txt"
.
You can get a list of all resources in an assembly using the Assembly.GetManifestResourceNames
Method.
这篇关于如何读取嵌入的资源文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!