C# – 在Listbox中保存值

我有以下代码:

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的保存位置?

解决方法:

ListBox不提供将其所有项目保存到文件的方法.此处提供了显示如何完成此操作的代码段:

C# code to save text from listbox to a text file — SOLVED–

上一篇:c-自定义Win32的“保存文件”对话框


下一篇:导出到Excel