日期:2014-05-19  浏览次数:21122 次

初学者一个StreamWriter的问题哈,不知道该怎么写
一个TEXTBOX   一个BUTTON     在TEXTBOX里输入信息后,点BUTTON后把TEXTBOX里的信息保存在
D盘下的123文件的123.TXT里,   如果没有123.TXT的话,自动创建

在次输入信息的时候记录在123.TXT里的第二行

代码该如何写?各位前辈帮帮忙吧
private   void   button1_Click(object   sender,   System.EventArgs   e)
{
  StreamWriter   sw   =new   StreamWriter();
                                      ....................................


}

------解决方案--------------------
string path = @ "d:\123.txt ";
System.IO.StreamWriter sw = null;
if(!System.IO.File.Exists(path))
sw = System.IO.File.CreateText(path);
else
sw = System.IO.File.AppendText(path);

sw.WriteLine(textBox1.Text);
sw.Close();
------解决方案--------------------
不好意思没看清搂主意思
paths = Directory.GetCurrentDirectory()+ "\\ Bak.txt ";
FileStream sour=new FileStream(paths ,FileMode .OpenOrCreate ,FileAccess .ReadWrite );// 如果没有就创建
StreamReader Myfile= new StreamReader(sour ,false,Encoding .Default );
myfile.writeline(textbox.txt);
myfile.close();
------解决方案--------------------
writeline
就行了
给你一段参考一下
private void SaveListBox(ListBox listBox,string defaultSaveName)
{
SaveFileDialog saveScanResult = new SaveFileDialog();
saveScanResult.Filter = "txt|*.txt ";
saveScanResult.Title = "Save the List ";
saveScanResult.FileName = defaultSaveName;
if (saveScanResult.ShowDialog() == DialogResult.OK)
//saveScanResult.ShowDialog();
{
if (saveScanResult.FileName != " ")
{
using (StreamWriter sw = new StreamWriter(saveScanResult.FileName))
{
sw.WriteLine( "The content of list is: ");
sw.WriteLine( "Save Date: ");
sw.WriteLine(DateTime.Now);
sw.WriteLine( "-------------------------- ");

for (int i = 0; i < listBox.Items.Count; i++)
{
sw.WriteLine(listBox.Items[i].ToString());
}
sw.Close();
MessageBox.Show( "The result has been saved! ");
}

}
}
}

存完结果是这个模样的
The content of list is:
Save Date:
2007-3-26 21:51:58
--------------------------
169.254.179.1::::::000000000000
169.254.179.2::::::000000000000
169.254.179.3::::::000000000000
169.254.179.4::::::000000000000
169.254.179.5::::::000000000000
169.254.179.6::::::000000000000