如何使用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();
}

当前回答

添加例如Testfile.sql项目菜单->财产->资源->添加现有文件

    string queryFromResourceFile = Properties.Resources.Testfile.ToString();

其他回答

在Visual Studio中,您可以通过项目财产(本例中为“分析”)的资源选项卡直接嵌入对文件资源的访问。

然后,可以通过

byte[] jsonSecrets = GoogleAnalyticsExtractor.Properties.Resources.client_secrets_reporter;

如果你需要它作为一个流,那么(从https://stackoverflow.com/a/4736185/432976 )

Stream stream = new MemoryStream(jsonSecrets)

我读取了一个嵌入式资源文本文件,使用:

    /// <summary>
    /// Converts to generic list a byte array
    /// </summary>
    /// <param name="content">byte array (embedded resource)</param>
    /// <returns>generic list of strings</returns>
    private List<string> GetLines(byte[] content)
    {
        string s = Encoding.Default.GetString(content, 0, content.Length - 1);
        return new List<string>(s.Split(new[] { Environment.NewLine }, StringSplitOptions.None));
    }

示例:

var template = GetLines(Properties.Resources.LasTemplate /* resource name */);

template.ForEach(ln =>
{
    Debug.WriteLine(ln);
});

添加例如Testfile.sql项目菜单->财产->资源->添加现有文件

    string queryFromResourceFile = Properties.Resources.Testfile.ToString();

某些VS.NET项目类型不会自动生成.NET(.resx)文件。以下步骤将资源文件添加到项目中:

右键单击项目节点并选择Add/NewItem,滚动到Resources File。在“名称”框中选择适当的名称,例如“资源”,然后单击“添加”按钮。资源文件Resources.resx被添加到项目中,并且可以作为解决方案资源管理器中的节点。实际上,创建了两个文件,还有一个自动生成的C#类Resources.Designer.cs。不要编辑它,它由VS维护。该文件包含一个名为Resources的类。

现在,您可以添加文本文件作为资源,例如xml文件:

双击Resources.resx。选择“添加资源”>“添加现有文件”,然后滚动到要包含的文件。将默认值“内部”保留为“访问修改”。图标表示新资源项。如果选中,属性窗格将显示其财产。对于xml文件,在属性Encoding下选择Unicode(UTF-8)–Codepage 65001,而不是默认的本地代码页。对于其他文本文件,请选择此文件的正确编码,例如代码页1252。对于像xml文件这样的文本文件,类Resources具有一个字符串类型的属性,该属性以包含的文件命名。如果文件名为例如RibbonManifest.xml,则属性的名称应为RibbonManifest。您可以在代码文件Resources.Designer.cs中找到确切的名称。像使用任何其他字符串属性一样使用字符串属性,例如:string xml=Resources.RibbonManifest。一般形式为ResourceFileName.IncludedTextFileName。不要使用ResourceManager.GetString,因为字符串属性的get函数已经完成了这一操作。

这是一个类,您可能会发现从当前程序集读取嵌入式资源文件非常方便:

using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;

public static class EmbeddedResourceUtils
{
    public static string ReadFromResourceFile(string endingFileName)
    {
        var assembly = Assembly.GetExecutingAssembly();
        var manifestResourceNames = assembly.GetManifestResourceNames();

        foreach (var resourceName in manifestResourceNames)
        {
            var fileNameFromResourceName = _GetFileNameFromResourceName(resourceName);
            if (!fileNameFromResourceName.EndsWith(endingFileName))
            {
                continue;
            }

            using (var manifestResourceStream = assembly.GetManifestResourceStream(resourceName))
            {
                if (manifestResourceStream == null)
                {
                    continue;
                }

                using (var streamReader = new StreamReader(manifestResourceStream))
                {
                    return streamReader.ReadToEnd();
                }
            }
        }

        return null;
    }
    
    // https://stackoverflow.com/a/32176198/3764804
    private static string _GetFileNameFromResourceName(string resourceName)
    {
        var stringBuilder = new StringBuilder();
        var escapeDot = false;
        var haveExtension = false;

        for (var resourceNameIndex = resourceName.Length - 1;
            resourceNameIndex >= 0;
            resourceNameIndex--)
        {
            if (resourceName[resourceNameIndex] == '_')
            {
                escapeDot = true;
                continue;
            }

            if (resourceName[resourceNameIndex] == '.')
            {
                if (!escapeDot)
                {
                    if (haveExtension)
                    {
                        stringBuilder.Append('\\');
                        continue;
                    }

                    haveExtension = true;
                }
            }
            else
            {
                escapeDot = false;
            }

            stringBuilder.Append(resourceName[resourceNameIndex]);
        }

        var fileName = Path.GetDirectoryName(stringBuilder.ToString());
        return fileName == null ? null : new string(fileName.Reverse().ToArray());
    }
}