要注意这个STREAM方式和一般的文件类的不同和相应的应用场合。。。
就像切肉刀和剔骨刀。。。同为刀,但大不一样。。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.IO; 11 12 namespace WindowsFormsApplication1 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 private void button1_Click(object sender, EventArgs e) 22 { 23 if (textBox1.Text == string.Empty) 24 { 25 MessageBox.Show("empty"); 26 } 27 else 28 { 29 saveFileDialog1.Filter = "text file(*.txt)|*.txt"; 30 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 31 { 32 StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, true); 33 sw.WriteLine(textBox1.Text); 34 sw.Close(); 35 textBox1.Text = string.Empty; 36 } 37 } 38 } 39 40 private void button2_Click(object sender, EventArgs e) 41 { 42 openFileDialog1.Filter = " textfile(*.txt)|*.txt"; 43 if (openFileDialog1.ShowDialog() == DialogResult.OK) 44 { 45 textBox1.Text = string.Empty; 46 StreamReader sr = new StreamReader(openFileDialog1.FileName); 47 textBox1.Text = sr.ReadToEnd(); 48 sr.Close(); 49 } 50 } 51 } 52 }