using System.IO;
private void button1_Click(object sender, System.EventArgs e)
{
FileStream fileStream = null;
StreamWriter streamWriter = null;
try
{
fileStream = new FileStream(Application.StartupPath + "\\" + "test.txt", FileMode.OpenOrCreate, FileAccess.Write);
streamWriter = new StreamWriter(fileStream, System.Text.Encoding.Default);
streamWriter.WriteLine(richTextBox1.Text);
streamWriter.Flush();
streamWriter.Close();
fileStream.Close();
MessageBox.Show("保存完毕");
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
if (streamWriter != null)
{
streamWriter.Close();
}
if (fileStream != null)
{
fileStream.Close();
}
}
}