如何将多个文本框值保存到.cgf文件中? [英] How do I save mutliple textbox value into .cgf file?

查看:81
本文介绍了如何将多个文本框值保存到.cgf文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五个文本框,我想在保存按钮单击时将每个文本框值保存到.cfg文件中,但我想在cfg文件中的下一行中的每个文本框值以相同的顺序,在wpf C#中。



谢谢。



我的尝试:



private void btnSave_Click(object sender,RoutedEventArgs e)

{

string filePath = @C:\ Users \sushil\Desktop \\ \\ WPFPractice \SaveTextBoxValueInTextFile \TextData.txt;

StreamWriter streamWriter = new StreamWriter(filePath,true);

streamWriter.WriteLine(txtNameData.Text);

streamWriter.WriteLine(txtAddData.Text);

streamWriter.WriteLine(txtDestData.Text);

MessageBox.Show(saved); < br $>
}



但它不起作用,

I have Five textboxes, I want to save each textbox value into .cfg file on save button click, but i want each textbox value in next line in cfg file in same sequence, in wpf C#.

thanks.

What I have tried:

private void btnSave_Click(object sender, RoutedEventArgs e)
{
string filePath = @"C:\Users\sushil\Desktop\WPFPractice\SaveTextBoxValueInTextFile\TextData.txt";
StreamWriter streamWriter = new StreamWriter(filePath, true);
streamWriter.WriteLine(txtNameData.Text);
streamWriter.WriteLine(txtAddData.Text);
streamWriter.WriteLine(txtDestData.Text);
MessageBox.Show("saved");
}

but it is not working,

推荐答案

添加以下内容m之前的行essage box:

Add the following lines before your message box :
...
streamWriter.Flush();
streamWriter.Close();
MessageBox.Show("saved");
}


你需要在<的每个实例上调用 System.IDisposable.Dispose code> StreamWriter ( StreamReader 和许多其他类型;规则是:查看类型是否实现 System。 IDisposable 并保证在您不再需要该实例时,始终会调用 Dispose



最好的方法是使用使用语句:
You need to call System.IDisposable.Dispose on each instance of StreamWriter (StreamReader and many other types; the rule is: see if the type implements System.IDisposable and guarantee that Dispose is always called as soon as you don't need the instance anymore).

The best way of doing so is using the using statement:
using (StreamWriter streamWriter = new StreamWriter(filePath, true)) {
   streamWriter.WriteLine(txtNameData.Text);
   streamWriter.WriteLine(txtAddData.Text);
   streamWriter.WriteLine(txtDestData.Text);
} // streamWriter.Dispose is automatically called here



这个 Dispose 的实现实际上负责关闭文件并刷新文件缓冲区,所以你只需要处理。



另请参阅:

https:/ /msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ],

IDisposable Interface(System) [ ^ ]。



-SA


This implementation of Dispose actually takes care of closing the file and flushing the file buffer, so disposal is all you need.

See also:
https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^],
IDisposable Interface (System)[^].

—SA


这篇关于如何将多个文本框值保存到.cgf文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆