如何读取文本文件行作为变量而不是文本

时间:2015-02-09 23:14:53

标签: c#

1-我在表单中有一些变量:

string BE1835 = "A";
string WB5884 = "B";
string S49807 = "C";
string D35950 = "D";

2-我的文本文件包含更多行:

line1
BE1835 
S49807
line4
line5 

3-我使用此代码阅读文本文件

    int counter = 0;
    string line;

    // Read the file and display it line by line.
    System.IO.StreamReader file =
        new System.IO.StreamReader(@JSS_File_Path_01);
    while ((line = file.ReadLine()) != null)
    {
        //read line 3,6,7,8,10
        if (counter == 1 || counter == 2)
        {
            final_cons_Code += line;
        }

        counter++;
    }

    file.Close();


    label4.Text = final_cons_Code;

5-结果:BE1835S49807

现在我的结果是AC而不是BE1835S49807

谢谢

1 个答案:

答案 0 :(得分:1)

更新:

// declare it

var variables = new Dictionary<string, string>();

// populate it

variables.Add("BE1835", "A");
variables.Add("WB5884", "B");
variables.Add("S49807", "C");
variables.Add("D35950", "D"); 

//retrieve a value

var alphabet = variables["BE1835"];

在您的用例中,您可以执行此操作

    //read line 3,6,7,8,10
    if (counter == 1 || counter == 2)
    {
        final_cons_Code += variables[line];
    }