简体   繁体   English

从资源读取文本文件

[英]Read text file from resource

I am trying the following code to split the words in a text file. 我正在尝试以下代码在文本文件中拆分单词。

The file is written like this: 该文件是这样写的:

Apple"Juice"Martini
Lemon"Juice"Party
Banana"Smoothie"Aligns

and the code following: 和以下代码:

        string resource_data = Properties.Resources.textfile;
        string[] result = resource_data.Split('"');
        foreach (string lines in result)
        {
            if(comboBox1.Text == result[0])
            {
                richTextBox2.Text = result[2];
            }
        }

Taken & edited from a c++ program I was working on which worked perfectly with the same txt file. 我正在使用的c ++程序进行了编辑,并与同一个txt文件完美配合。

             String^ resource_data = "textfile.txt";
             try
             {
                 StreamReader^ DataIn = File::OpenText(resource_data);
                 String^ DataStr;
                 int count = 0;
                 array<String^>^ result;
                 array<Char>^ separ = gcnew array<Char>{'"'};

                 while((DataStr = DataIn->ReadLine()) != nullptr)
                 {
                     count++;
                     result = DataStr->Split(separ);
                     if(comboBox1->Text == result[0]) // result[0] = Name
                     {

What the code does.. Read each line as it own. 代码的作用。逐行读取每一行。 Gives first word in each line result[0] as second word on each line is result[1] etc. When I select a word in the combobox I check if it is the same as in the text file and that line is used in result[x]. 给每一行result [0]中的第一个单词,因为每一行中的第二个单词都是result [1]等。当我在组合框中选择一个单词时,我检查它是否与文本文件中的单词相同,并在结果中使用该行[X]。 But in the C# it gives ALL words own result[x] and lines does not matter. 但是在C#中,它为所有单词提供了自己的result [x],而行并不重要。

How can I make the following code in c++ to work in C# but having the text file in the resources.resx? 如何在c ++中使以下代码在C#中工作,但在resources.resx中具有文本文件?

I think I see what the problem is. 我想我明白了问题所在。 You first need to split the string resource_data into separate lines. 您首先需要将字符串resource_data拆分为单独的行。 You could do this by splitting resource_data on the new line character(s): 您可以通过在新的换行符上拆分resource_data来做到这一点:

string[] lines = resource_data.Split(new string[1] { Environment.NewLine }, StringSplitOptions.None);
foreach (var line in lines)
{
    string[] parts = line.Split('"');
    if (comboBox1.Text == result[0])
    {
        richTextBox2.Text = result[2];
    }
}

You could also do this using a StringReader: 您也可以使用StringReader进行此操作:

using (StringReader reader = new StringReader(resource_data))
{
    while (reader.Peek() >= 0)
    {
        string[] parts = reader.ReadLine().Split('"');
        if (comboBox1.Text == result[0])
        {
            richTextBox2.Text = result[2];
        }
    }
}

Additionally if you are only storing the path to the file in the resources, you could open the file and read from it: 另外,如果仅在资源中存储文件的路径,则可以打开文件并从中读取:

using (StreamReader reader = File.OpenText(resource_path)) // path to file
{
    while (reader.Peek() >= 0)
    {
        string[] parts = reader.ReadLine().Split('"');
        if (comboBox1.Text == result[0])
        {
            richTextBox2.Text = result[2];
        }
    }
}

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

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