简体   繁体   English

从C#Resources读取文本文件

[英]Read text file from C# Resources

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...."这样的答案,但我的程序没有选择汇编。

Try something like this : 尝试这样的事情:

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

Where 哪里

在此输入图像描述

You need to include using System.Reflection; 您需要using System.Reflection;包含using System.Reflection; in your header in order to get access to Assembly. 在您的标题中,以便访问Assembly。 This is only for when you mark a file as "Embedded Resource" in VS. 这仅适用于在VS中将文件标记为“嵌入式资源”的情况。

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

As long as you include 'using System.Reflection;' 只要你包含'using System.Reflection;' you can access Assembly like this: 您可以像这样访问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
    }
}

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){
.....
}
        [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. 您将资源文件作为字节数组获取,因此您可以使用WriteAllBytes创建新文件。 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. 如果您不知道在哪里可以编写文件(权限和访问的原因),您可以使用Path.GetTempPath()来使用PC临时文件夹来编写新文件,然后您可以从那里复制或工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM