gpt4 book ai didi

.net - 如何读取嵌入的资源文本文件

转载 作者:行者123 更新时间:2023-12-02 00:02:24 28 4
gpt4 key购买 nike

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

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 Method :

  1. 添加以下用法

     using System.IO;
    using System.Reflection;
  2. 设置相关文件的属性:
    参数Build Action,值为Embedded Resource

  3. 使用以下代码

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

<小时/>

仅从文件名中获取resourceName(绕过命名空间)的简单技巧:

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

一个完整的例子:

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

或作为异步扩展方法:

internal static class AssemblyExtensions
{
public static async Task<string> ReadResourceAsync(this Assembly assembly, string name)
{
// Determine path
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(stream);
return await reader.ReadToEndAsync();
}
}

// Usage
string resourceText = await Assembly.GetExecutingAssembly().ReadResourceAsync("myResourceName");

关于.net - 如何读取嵌入的资源文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3314140/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com