回答
要保存
// fetch the selected Text from your list
string textToRight = listBox1.SelectedItem.ToString();
// Write to a file
StreamWriter sr = File.CreateText(@"testfile.txt");
sr.Write(textToRight);
sr.Close();
消息
// display Message
MessageBox.Show("Information Saved Successfully");
**您忘记关闭StreamWriter **。 – SLaks 2010-02-19 00:39:41
已修复,谢谢。 – 2010-02-19 00:45:26
一个SaveFileDialog
用于与ShowDialog()
展示给用户,如果它是成功的,使用它的OpenFile()
得到(文件)Stream
那你写信给。 msdn page上有一个例子。
A ListBox
可以通过其Items
属性进行访问,该属性只是其上的项目集合。
你有一些事情在那里 - 确保你把它们分开,例如,
- 获取列表框中的内容
- 附加信息
- 写文件
请注意!有例外,而保存文件,你可以得到,看文档和处理这些莫名其妙的无数......
// Get list box contents
var sb = new StringBuilder();
foreach (var item in lstBox.Items)
{
// i am using the .ToString here, you may do more
sb.AppendLine(item);
}
string data = sb.ToString();
// Append Info
data = data + ????....
// Write File
void Save(string data)
{
using(SaveFileDialog saveFileDialog = new SaveFileDialog())
{
// optional
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
//saveFileDialog.Filter = ???;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog.Filename);
MessageBox.Show("ok", "all good etc");
}
else
{
// not good......
}
}
}
修复你的代码块格式,请...;) – IAbstract 2010-02-19 00:53:50
是啊 - 那真是丑陋! (完成) – 2010-02-19 00:56:46
这应该这样做。
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog f = new OpenFileDialog();
f.ShowDialog();
ListBox l = new ListBox();
l.Items.Add("one");
l.Items.Add("two");
l.Items.Add("three");
l.Items.Add("four");
string textout = "";
// assume the li is a string - will fail if not
foreach (string li in l.Items)
{
textout = textout + li + Environment.NewLine;
}
textout = "extra stuff at the top" + Environment.NewLine + textout + "extra stuff at the bottom";
File.WriteAllText(f.FileName, textout);
MessageBox.Show("all saved!");
}
'OpenFileDialog'?或'SaveFileDialog'? – spajce 2013-02-03 00:10:49
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一个类型。
可能的重复:http://stackoverflow.com/questions/3336186/saving-listbox-items-to-file?rq=1 – 2013-06-28 05:50:50
完全真棒.. – 2014-04-21 09:55:28
- 1. C#将列表框中的所有项目保存为文本文件
- 2. 保存列表框项目文件
- 3. 如何保存文本文件中的列表框项目
- 4. 将Windows窗体列表框保存为文本文件C#
- 5. 如何保存文本文件中的列表框项?
- 6. 将c#项目保存为一个单独的文本文件?
- 7. 将文本和文本框保存为文本文件
- 8. 如何将列表视图中的所有项目保存到文本文件
- 9. 将一个目录列表保存到一个文本文件
- 10. 将文本框更新为选定项目列表框
- 11. 如何将特定文本框保存为文本文件?
- 12. C# - 将多行文本框保存为文本文件
- 13. 将列表框项目保存为使用linq和c的xml文件中的子项目#
- 14. 将R列表保存为文本文件
- 15. 将文件保存到项目文件夹并保存路径
- 16. 将文本文件保存到Android项目中的位置
- 17. 检查列表框项目从文本框的值存在
- 18. Lisbox项目保存为带有“,”的文本文件seprarate
- 19. ASP.NET列表框选择的项目改变文本框文本
- 20. 使用python将文件列表保存到文本文件中
- 21. 将Excel工作表文本保存为文本文件VBA
- 22. 如何保存一个文本文件中的列表框
- 23. 在文本文件中保存列表
- 24. 如何将每个列表框项目保存到一个xml文件中
- 25. 将列表框中列出的文本文件内容显示为文本框?
- 26. 如何在asp.net [服务器端]的文本文件中保存列表框项?
- 27. 保存Properties.Settings.Default项目列表,更改值,然后将其保存到文件
- 28. 将项目保存到XML文件
- 29. WP7获取列表框项目文本
- 30. 文本框文本到列表框项目vb.net
@roller - 这个问题解决了吗? – 2010-02-23 02:56:04