创建一个文件夹用于写入UTF-8编码的文件

实现效果:

  

知识运用:

  File类的CreateText方法

  StreamWriter类的WriteLine方法

实现代码:

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请填写文件路径!");
                return;
            }
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("请输入文件内容!");
                return;
            }
            if (!File.Exists(textBox1.Text))
            {
                StreamWriter sw = File.CreateText(textBox1.Text);
                sw.WriteLine(textBox2.Text);
                sw.Close();
                MessageBox.Show("文件创建成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else
            { MessageBox.Show("文件存在!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); }
        }

  

原文地址:https://www.cnblogs.com/feiyucha/p/10252627.html