向文件中写入追加的数据

实现效果:

  

知识运用:

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

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/10253350.html
今日推荐