如何读取嵌入的资源文本文件 [英] How to read embedded resource text file

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

问题描述

我如何读取使用的StreamReader,并返回一个字符串嵌入的资源(文本文件)?我现在的脚本使用Windows窗体和文本框,它允许用户查找和未嵌入一个文本文件替换文本。

 私人无效的button1_Click(对象发件人,EventArgs的)
{
    StringCollection strValues​​ToSearch =新StringCollection();
    strValues​​ToSearch.Add(苹果);
    串stringToReplace;
    stringToReplace = textBox1.Text;

    StreamReader的FileReader对象=新的StreamReader(@C:\ MyFile.txt的);
    串FileContents;
    FileContents = FileReader.ReadToEnd();
    FileReader.Close();
    的foreach(字符串s的strValues​​ToSearch)
    {
        如果(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方法:

  1. 添加使用以下

     使用的System.Reflection;
     

  2. 设置相关文件的属性:
    以价值嵌入的资源参数生成操作

  3. 使用下面的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方法。

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:

  1. Add the following using

    using System.Reflection;
    

  2. Set property of relevant file:
    Parameter Build Action with value Embedded Resource

  3. Use 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屋!

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