有没有办法使用分隔符拆分文本文件数据并将其显示在文本框中?

Is there any way to split the text file data using delimiters ans show it in textboxes?

这里是保存在文本文件中的原始数据

zeeshan
adnan
shams
jawaid

我希望通过搜索名字将这些数据放在单独的 4 个文本框中。

这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
    FileStream file = new FileStream(@"C:\Users\Zeeshan\Downloads\a.txt", FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(file);
    sr.ReadLine();
    var textLines = File.ReadAllLines(@"C:\Users\Zeeshan\Downloads\a.txt");

    foreach (var Line in textLines)
    {
        // (str, "\n\s*")
        //string[] dataArray = Regex.Split('n');
        string[] dataArray = Line.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
        dataArray[0].CompareTo(comboBox1.SelectedItem);
        if (dataArray[0] == comboBox1.SelectedItem.ToString())
        {
            textBox1.Text = (dataArray[0]);
            textBox2.Text = (dataArray[1]);
            textBox3.Text = (dataArray[2]);
            textBox4.Text = (dataArray[3]);
        }
    }
}

此代码的问题在于,当我按下按钮时,它在索引 [1] 处显示异常。

错误是:

"Index was outside the bounds of the array."

File.ReadAllLines(...) returns 每个 "new line" 一个字符串。这意味着每行中不会有 System.Environment.NewLine。当您尝试访问行部分时,这会正确地导致 "index out of range" 异常。

// I'll walk you though your code...
private void button1_Click(object sender, EventArgs e)
{
    /* this is not needed
    FileStream file = new FileStream(@"C:\Users\Zeeshan\Downloads\a.txt", FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(file);
    sr.ReadLine();
    */

    // this returns an array of all of the lines you have in your text file 
    //   each line is a new element in the array
    var textLines = File.ReadAllLines(@"C:\Users\Zeeshan\Downloads\a.txt");

    // here you try to loop the lines in the text file.
    foreach (var Line in textLines)
    {
        // (str, "\n\s*")
        //string[] dataArray = Regex.Split('n');

        // here you try to split the line on a new line CR + LF (which doesn't exist) so dataArray only has one element
        string[] dataArray = Line.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

        // this line is useless
        dataArray[0].CompareTo(comboBox1.SelectedItem);

        // this will be true if the current line is the same value as your drop down.
        if (dataArray[0] == comboBox1.SelectedItem.ToString())
        {
            // this line will work
            textBox1.Text = (dataArray[0]);

            // these will always fail.
            textBox2.Text = (dataArray[1]);
            textBox3.Text = (dataArray[2]);
            textBox4.Text = (dataArray[3]);
        }
    }
}

...如果您希望文本框包含文本文件中的 4 个值,那么只需执行此操作...

private void button1_Click(object sender, EventArgs e)
{
    var textLines = File.ReadAllLines(@"C:\Users\Zeeshan\Downloads\a.txt");
    textBox1.Text = (textLines[0]);
    textBox2.Text = (textLines[1]);
    textBox3.Text = (textLines[2]);
    textBox4.Text = (textLines[3]);
}

你的代码有几个问题:

// FileStream file = new FileStream(@"C:\Users\Zeeshan\Downloads\a.txt", FileMode.Open, FileAccess.Read);
// StreamReader sr = new StreamReader(file);
// sr.ReadLine();
// Above three lines are redundant, because you read all of the text inside next line (ReadAllLines)
var textLines = File.ReadAllLines(@"C:\Users\Zeeshan\Downloads\a.txt");

// This foreach loop is wrong, since textLines already is array of lines from text file (each element is already single line without Environment.NewLine characters)
/*foreach (var Line in textLines)
{
    // (str, "\n\s*")
    //string[] dataArray = Regex.Split('n');
    string[] dataArray = Line.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    // This CompareTo is redundant also, since you are not using its result
    dataArray[0].CompareTo(comboBox1.SelectedItem);
    if (dataArray[0] == comboBox1.SelectedItem.ToString())
    {
        textBox1.Text = (dataArray[0]);
        textBox2.Text = (dataArray[1]);
        textBox3.Text = (dataArray[2]);
        textBox4.Text = (dataArray[3]);
    }
}*/

// Instead of foreachloop above, you just need to put values inside textboxes:
if (textLines[0] == comboBox1.SelectedItem.ToString())
{
    textBox1.Text = (textLines[0]);
    textBox2.Text = (textLines[1]);
    textBox3.Text = (textLines[2]);
    textBox4.Text = (textLines[3]);
}