如何读取嵌入式资源文本文件

How to read embedded resource text file(如何读取嵌入式资源文本文件)

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

问题描述

如何使用 StreamReader 读取嵌入式资源(文本文件)并将其作为字符串返回?我当前的脚本使用 Windows 表单和文本框,允许用户在未嵌入的文本文件中查找和替换文本.

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();
}

推荐答案

您可以使用 Assembly.GetManifestResourceStream 方法:

  1. 添加以下用法

  1. Add the following usings

using System.IO;
using System.Reflection;

  • 设置相关文件的属性:
    参数Build Action,值为Embedded Resource

    使用以下代码

    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 是嵌入在 assembly 中的资源之一的名称.例如,如果您嵌入了一个名为 "MyFile.txt" 的文本文件,该文件位于具有默认命名空间 "MyCompany.MyProduct" 的项目的根目录中,则 >resourceName"MyCompany.MyProduct.MyFile.txt".您可以使用 获取程序集中所有资源的列表Assembly.GetManifestResourceNames 方法.

    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.

    <小时>

    仅从文件名中获取 resourceName 的明智之举(通过命名空间的东西):


    A no brainer astute to get the resourceName from the file name only (by pass the namespace stuff):

    string resourceName = assembly.GetManifestResourceNames()
      .Single(str => str.EndsWith("YourFileName.txt"));
    

    <小时>

    一个完整的例子:


    A complete example:

    public string ReadResource(string name)
    {
        // Determine path
        var assembly = Assembly.GetExecutingAssembly();
        string resourcePath = name;
        // Format: "{Namespace}.{Folder}.{filename}.{Extension}"
        if (!name.StartsWith(nameof(SignificantDrawerCompiler)))
        {
            resourcePath = assembly.GetManifestResourceNames()
                .Single(str => str.EndsWith(name));
        }
    
        using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
        using (StreamReader reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }
    }
    

    这篇关于如何读取嵌入式资源文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

  • 本文标题为:如何读取嵌入式资源文本文件

    基础教程推荐