如何使用 SaveFileDialog
将我的 listbox
项的内容保存到文本文件?
我还想向文本文件中添加其他信息,并添加一个 MessageBox
表示成功时已保存。
最佳答案
var saveFile = new SaveFileDialog();
saveFile.Filter = "Text (*.txt)|*.txt";
if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (var sw = new StreamWriter(saveFile.FileName, false))
foreach (var item in listBox1.Items)
sw.Write(item.ToString() + Environment.NewLine);
MessageBox.Show("Success");
}
另请注意 StreamWriter
的类型为 Encoding .
关于c# - 将列表框的项目保存到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2293298/