向文件中写入追加的数据

Posted on 2019-01-11 02:33  努力成长静待花开  阅读(621)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:

  StramWrite类的构造方法和Write方法

实现代码:

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("文件还未选择");
                return;
            }
            if (string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                MessageBox.Show("内容不能为空");
                return;
            }
            try
            {
                StreamWriter sw = new StreamWriter(textBox1.Text,true);
                sw.Write(textBox2.Text);
                sw.Close();
                MessageBox.Show("文件写入成功!");
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }