从 C# 资源读取文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15722455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:44:15  来源:igfitidea点击:

Read text file from C# Resources

c#file-ioresourcesstreamreader

提问by Dobz

I need to read a file from my resources and add it to a list. my code:

我需要从我的资源中读取一个文件并将其添加到列表中。我的代码:

private void Form1_Load(object sender, EventArgs e)
{
    using (StreamReader r = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("myProg.myText.txt")))
    {
        //The Only Options Here Are BaseStream & CurrentEncoding
    }
}

Ive searched for this and only have gotten answers like "Assembly.GetExecutingAssembly...."but my program doesnt have the option of Assembly.?

我已经搜索过这个,但只得到了类似的答案,"Assembly.GetExecutingAssembly...."但我的程序没有装配选项。?

采纳答案by Parimal Raj

Try something like this :

尝试这样的事情:

string resource_data = Properties.Resources.test;
List<string> words = resource_data.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).ToList();

Where

在哪里

enter image description here

在此处输入图片说明

回答by Pete Garafano

You need to include using System.Reflection;in your header in order to get access to Assembly. This is only for when you mark a file as "Embedded Resource" in VS.

您需要包含using System.Reflection;在标题中才能访问程序集。这仅适用于在 VS 中将文件标记为“嵌入式资源”。

var filename = "MyFile.txt"
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNameSpace." + filename));

As long as you include 'using System.Reflection;' you can access Assembly like this:

只要您包括“使用 System.Reflection;” 你可以像这样访问Assembly:

Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace." + filename);

Or if you don't need to vary filename just use:

或者,如果您不需要更改文件名,只需使用:

Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace.MyFile.txt");

The full code should look like this:

完整代码应如下所示:

using(var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("myProg.m??yText.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        // Do some stuff here with your textfile
    }
}

回答by thaFaxGuy

Just to follow on this, AppDeveloper solution is the way to go.

只是为了遵循这一点,AppDeveloper 解决方案是要走的路。

string resource_data = Properties.Resources.test;
string [] words = resource_data.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
foreach(string lines in words){
.....
}

回答by Danny Fallas

        [TestCategory("THISISATEST")] 
        public void TestResourcesReacheability() 
        { 
            byte[] x = NAMESPACE.Properties.Resources.ExcelTestFile; 
            string fileTempLocation = Path.GetTempPath() + "temp.xls"; 
            System.IO.File.WriteAllBytes(fileTempLocation, x);   

            File.Copy(fileTempLocation, "D:\new.xls"); 
        }

You get the resouce file as a byte array, so you can use the WriteAllBytes to create a new file. If you don't know where can you write the file (cause of permissions and access) you can use the Path.GetTempPath() to use the PC temporary folder to write the new file and then you can copy or work from there.

您将资源文件作为字节数组获取,因此您可以使用 WriteAllBytes 创建一个新文件。如果您不知道在哪里可以写入文件(权限和访问的原因),您可以使用 Path.GetTempPath() 使用 PC 临时文件夹写入新文件,然后您可以从那里复制或工作。