我有以下代码:
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。
问题
- 如何以与 RichTextBox 类似的方式保存 ListBox?
- 有什么方法可以编辑此代码以使其让用户选择 ListBox 的保存位置?