C#如何在一个文本文件中写多行? [英] C# how to write multiple lines in a text file?

查看:881
本文介绍了C#如何在一个文本文件中写多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按下一个按钮,该按钮将从textbox4和textbox5中获取文本,以将其写入文本文件.但是,当我再次按它向文本文件中添加新信息时,它只是将旧文本替换为新文本.每次按下按钮时,如何在第一行下面写一行?

I am trying to press a button that takes the text from textbox4 and textbox5 to write it to a text file. BUT when I press it again to add new info to the text file it just replaces the old text in with the new. How do I get it to write another line below the first one each time I press the button?

这是我到目前为止的代码

This is the code I have so far

    private void button5_Click(object sender, EventArgs e)
    {
        try
        {
            xuidspath = @"c:\xuids.txt";
            ListViewItem lvi = new ListViewItem();
            lvi.Text = textBox4.Text;
            lvi.SubItems.Add(textBox5.Text);
            listXuid.Items.Add(lvi);
            TextWriter xuids = new StreamWriter(xuidspath);
            xuids.WriteLine(textBox4.Text + "-" + textBox5.Text);
            textBox5.Clear();
            textBox4.Clear();
            xuids.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

有什么想法吗?

推荐答案

打开文件进行追加.

FileStream xuids = new FileStream(xuidspath, FileMode.Append);

这篇关于C#如何在一个文本文件中写多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆