我有以下代码:
SaveFileDialog saveGC1File = new SaveFileDialog();
private void GCSavePlacementOneButton_Click(object sender, EventArgs e)
{
// Initialize the SaveFileDialog to specify the .txt extension for the file.
saveGC1File.DefaultExt = "*.txt";
saveGC1File.Filter = ".txt Files|*.txt|All Files (*.*)|*.*";
saveGC1File.RestoreDirectory = true;
try
{
string placementOneSave = placementOneListBox.Items.ToString();
// Save the contents of the formattedTextRichTextBox into the file.
if (saveGC1File.ShowDialog() == DialogResult.OK && saveGC1File.FileName.Length > 0)
placementOneSave.SaveFile(saveGC1File.FileName, RichTextBoxStreamType.PlainText);
// Throws a FileNotFoundException otherwise.
else
throw new FileNotFoundException();
}
// Catches an exception if the file was not saved.
catch (Exception)
{
MessageBox.Show("There was not a specified file path.", "Path Not Found Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
但是,使用Visual Studio 2010,“if”循环中的行指出:
"placementOneSave.SaveFile(saveGC1File.FileName, RichTextBoxStreamType.PlainText);"
在“SaveFile”下面有一个红色行。我之前使用过此代码来保存文件,并且不确定它为什么不能用于ListBox。
的问题 的
答案 0 :(得分:2)
ListBox不提供将所有项目保存到文件的方法。有关如何完成此操作的代码段,请访问:
C# code to save text from listbox to a text file -- SOLVED--