回答
要保存
// 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. 如何保存文本文件中的列表框項目
- 2. 將Windows窗體列表框保存爲文本文件C#
- 3. 將c#項目保存爲一個單獨的文本文件?
- 4. 如何將列表視圖中的所有項目保存到文本文件
- 5. 將文本框更新爲選定項目列表框
- 6. 將R列表保存爲文本文件
- 7. 將文本文件保存到Android項目中的位置
- 8. 檢查列表框項目從文本框的值存在
- 9. ASP.NET列表框選擇的項目改變文本框文本
- 10. 將Excel工作表文本保存爲文本文件VBA
- 11. 如何保存一個文本文件中的列表框
- 12. 如何將每個列表框項目保存到一個xml文件中
- 13. 將asp.net列表框項目傳遞給文本框
- 14. C#將多個文本框保存爲一個.txt文件
- 15. 選項可將輸出結果保存爲文本文件(Python)
- 16. 將列表中的每個項目寫入文本文件
- 17. 如何將文本從文本框保存到文件(Tkinter)
- 18. 如何將多行文本框保存爲文本文件的一行?
- 19. Powershell列表框項目項目文本的不同值?
- 20. 如何將多個列表框值保存到單個文本文件中C#
- 21. 將RichText Box的文本保存爲文本文件
- 22. 將數據從列表保存到文本文件
- 23. 如何將此文本文件保存到python列表中
- 24. 將文本文件內容保存到鏈接列表
- 25. 如何將列表保存到文本文件?
- 26. 將Visual Studio項目中的所有文件保存爲UTF-8
- 27. 如何將git存儲保存爲文件或文件目錄?
- 28. 在c中的文本文件保存ListView項目#
- 29. 何處保存與項目相關的文本文件?
- 30. 富文本框保存文件
@roller - 這個問題解決了嗎? – 2010-02-23 02:56:04