是否有任何方法可以使用分隔符分割文本文件数据并将其显示在文本框中

本文关键字:显示 文本 数据 文件 方法 任何 可以使 分割文本 分隔符 是否 | 更新日期: 2023-09-27 18:25:30

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

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]处显示一个异常。

错误为:

"索引超出了数组的界限。"

是否有任何方法可以使用分隔符分割文本文件数据并将其显示在文本框中

您的代码中有几个问题:

// 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]);
}

File.ReadAllLines(...)为每个"新行"返回一个字符串。这意味着在每一行中将不存在System.Environment.NewLine。当您尝试访问线条部分时,这正确地导致了"索引超出范围"异常。

// 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]);
}