C#怎么通过点击button控件保存textBox内容到txt文件

窗体上只有一个textBox和一个button,点击button后,textBox中的内容就自动保存了,这个是怎么写的,请高手指教

button事件中写下面的代码就可以啦!
FileStream fs = new FileStream(@"Login.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.Write(this.textBox1.text);
sw.Close();
fs.Close();
@"Login.txt"这是文本路径,可以自己写绝对路径,要引入system.IO命名空间,不懂用法查msdn追问

别人教我的方法只有用了一句哒

追答

那你就用那个啥,最重要要自己能够用得来,理解得到,多种方法都要学到。还要会对比啥,这些方法都用了什么知识。我怎么用对我自己的程序更好呢?

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-23
你指的保存是保存到哪里?cookie中、Session中还是数据库中?
你双击button,在事件中添加代码
Session["key"]=textBox1.text; //就可以保存到Session中了
HttpCookie mycookie=new HttpCookie("key");
mycookie.Values.Add("KeyValue1",textBox1.Text);
mycookie.Expires=System.DateTime.Now.AddDays(1);
Response.Cookies.Add(mycookie);
如果是添加到数据库中,现按照linq的写法举例
Using(DataClasses1 db=new DataClasses1())
{
Table1 t=new Table1();
t.UserName=textBox1.Text;
db.t.InsertOnSubmit(t);
db.SubmitChanges();
}追问

别人教我的方法只有用了一句哒

第2个回答  2011-11-23
在button的按钮事件里写下面的代码即可,txt文件名及路径可根据自己的需要修改。
using (StreamWriter sw = new StreamWriter(Server.MapPath("/") + "txt/contact.txt", false))
{
sw.Write(textBox.text);
}
第3个回答  2011-11-23
string filePath = "C:\Test.txt";
if(System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath)
System.IO.File.AppendAllText(filePath, textBox.Text);

这是最偷懒的方法追问

别人教我的方法只有用了一句哒

追答

问题是,你懂了么?...
写C#这一类OO程序,程序长短根本就不是一个标准.

第4个回答  2011-11-23
你是哪不会呢? 1.获取到 textbox值 2. 触发button的 click事件。3.在事件中将textbox值 存入到 txt文件
相似回答