如何在文本文件中将多行写入不同的文本框?

时间:2016-01-04 02:24:24

标签: c#

我从框中写到txt文件的内容

private void save2_Click(object sender, EventArgs e)
{
    Stream myStream;
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Filter = "Textdokument|*.txt";
    sfd.FilterIndex = 1;
    sfd.RestoreDirectory = true;

    if (sfd.ShowDialog() == DialogResult.OK)
    {
        if ((myStream = sfd.OpenFile()) != null)
        {
            StreamWriter sw = new StreamWriter(myStream);

            sw.WriteLine(TextBoxCardname1.Text);
            sw.WriteLine(TextBoxCardname2.Text);
            sw.WriteLine(TextBoxCardname3.Text);
            sw.WriteLine(TextBoxCardname4.Text);
            sw.WriteLine(TextBoxCardname5.Text);
            sw.WriteLine(TextBoxCardname6.Text);
            sw.WriteLine(TextBoxCardname7.Text);
            sw.WriteLine(TextBoxCardname8.Text);
            sw.WriteLine(TextBoxCardname9.Text);
            sw.WriteLine(TextBoxCardname10.Text);
            sw.WriteLine(TextBoxCardname11.Text);
            sw.WriteLine(TextBoxCardname12.Text);
            sw.WriteLine(TextBoxCardname13.Text);
            sw.WriteLine(TextBoxCardname14.Text);
            sw.WriteLine(TextBoxCardname15.Text);
            sw.WriteLine(TextBoxCardname16.Text);
            sw.WriteLine(TextBoxCardname17.Text);
            sw.WriteLine(TextBoxCardname18.Text);
            sw.WriteLine(TextBoxCardname19.Text);
            sw.WriteLine(TextBoxCardname20.Text);
            sw.WriteLine(TextBoxCardname21.Text);
            sw.WriteLine(TextBoxCardname22.Text);
            sw.WriteLine(TextBoxCardname23.Text);
            sw.WriteLine(TextBoxCardname24.Text);
            sw.WriteLine(TextBoxCardname25.Text);
            sw.WriteLine(TextBoxCardname26.Text);
            sw.WriteLine(TextBoxCardname27.Text);
            sw.WriteLine(TextBoxCardname28.Text);
            sw.WriteLine(TextBoxCardname29.Text);
            sw.WriteLine(TextBoxCardname30.Text);
            sw.Close();
            myStream.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

假设您的问题意味着您想要获取文本文件的每一行的内容并将其写入相应的Managed;这对你有用:

textbox.text

对于您正在阅读的每一行,您可以指定要跳过的行数,以及要读取的行数(即TextBoxCardname1.Text = System.IO.File.ReadLines("Your Text File").Skip(0).Take(1).First(); TextBoxCardname2.Text = System.IO.File.ReadLines("Your Text File").Skip(1).Take(1).First(); TextBoxCardname3.Text = System.IO.File.ReadLines("Your Text File").Skip(2).Take(1).First(); //...etc 表示您将跳过文本文件中的第一行,并且阅读第二行)。

如果您正在尝试获取textbox.text并将其写入文本文件(从您的问题中很难说出来)这是一种简单但混乱且劳动密集的方法:

Skip(1), Take (1)

此方法是一种非常简单的方法,可以写入文本文件中的某一行。 var lines = System.IO.File.ReadAllLines("Your text file"); lines[0] = TextBoxCardname1.text); System.IO.File.WriteAllLines("Your text file", lines); var lines = System.IO.File.ReadAllLines("Your text file"); lines[1] = TextBoxCardname2.text); System.IO.File.WriteAllLines("Your text file", lines); var lines = System.IO.File.ReadAllLines("Your text file"); lines[2] = TextBoxCardname3.text); System.IO.File.WriteAllLines("Your text file", lines); //...etc 表示文本文件中的第4行,因为行号从零开始lines[3](文本文件中的第一行)。