日期:2014-05-18 浏览次数:20752 次
public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 全局变量,用来存放TXT路径 /// </summary> private string txtPath = String.Empty; /// <summary> /// 打开文本文档 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Btn_Opne_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(); //得到文本路径,显示出来 txtPath = openFileDialog1.FileName; if (txtPath == "") return; StreamReader sr = new StreamReader(txtPath, System.Text.Encoding.Default); string strText = sr.ReadToEnd(); sr.Close(); sr.Dispose(); textBox1.Text = strText; } /// <summary> /// 保存文本 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Btn_Save_Click(object sender, EventArgs e) { WriteTxt(textBox1.Text, txtPath); } /// <summary> /// 写入 /// </summary> /// <param name="strMemo">写入的字符串</param> /// <param name="path">文本所在的路径</param> public void WriteTxt(string strMemo, string path) { StreamWriter sw = new StreamWriter(path, true); sw.WriteLine(strMemo); sw.Flush(); sw.Close(); sw.Dispose(); } }