實現效果:

知識運用:
File類的OpenWrite方法 //實現打開現有文件以進行寫入
public static FileStream OpenWrite (string path)
Encoding抽象類的GetBytes方法 //將指定的字符串中的所有字符編碼為一個字節序列
public virtual byte[] GetBytes (string s)
實現代碼:
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("請設置文件");
return;
}
try
{
FileStream fs = File.OpenWrite(textBox1.Text);
byte[] b = Encoding.UTF8.GetBytes(textBox2.Text);
fs.Write(b,0,b.Length);
fs.Close();
MessageBox.Show("寫入成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
