Reputation: 1636
I have the following code:
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);
}
}
However, using Visual Studio 2010, the line in the "if" loop that states:
"placementOneSave.SaveFile(saveGC1File.FileName, RichTextBoxStreamType.PlainText);"
Has a red line under the "SaveFile".. I have used this code to save a file before and am unsure why it will not work for a ListBox.
QUESTIONS
Upvotes: 4
Views: 2308
Reputation: 5340
ListBox does not provide a method to save all its items to a file. The code snippet showing how this can be done is available here:
C# code to save text from listbox to a text file -- SOLVED--
Upvotes: 2