1

大家好,这里有点麻烦。

所以我有一个加载文件的加载按钮和一个保存文件的保存按钮。我还有一个关闭程序的退出按钮。我需要帮助的是当我关闭程序时,我不会检查是否有任何未关闭的 StreamReader 或 StreamWriter 东西。

这是我目前所拥有的:在顶部,我清除了这些人

    bool isDirtyBoolean = false;
    string moreData = "";

我的加载按钮看起来像这样

    private void loadToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //begin in the project folder
        openFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();

        //display the file open dialog box
        DialogResult responseDialogResult;
        responseDialogResult = openFileDialog1.ShowDialog();

        if (responseDialogResult != DialogResult.Cancel)
        {   //check that user did not click the cancel button
            //create a streamreader object for the selected file, 
            //read the file contents,
            //and display each line of the file in the list box

            StreamReader nameStreamReader = new StreamReader(openFileDialog1.FileName);

            while (nameStreamReader.Peek() != -1)
            {
                freindsDataListBox.Items.Add(nameStreamReader.ReadLine());

            }

            nameStreamReader.Close();
            isDirtyBoolean = true;
        }
    }

我的保存按钮看起来像这样

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //begin in the project folder
        saveFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();

        //display the file save dialog 
        DialogResult responseDialogResult;
        responseDialogResult = saveFileDialog1.ShowDialog();

        if (responseDialogResult != DialogResult.Cancel)
        {   //check that user did not click the cancel button
            //create a streamWriter object and 
            //then write out the list box items to the file

            StreamWriter nameStreamWriter = new StreamWriter(saveFileDialog1.FileName);

            int count = freindsDataListBox.Items.Count;
            for (int i = 0; i < count; i++)
            {
                nameStreamWriter.WriteLine(freindsDataListBox.Items[i]);
            }

            nameStreamWriter.Close();
            isDirtyBoolean = true;
            freindsDataListBox.Items.Clear();
        }
    }

我的退出按钮看起来像这样

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (isDirtyBoolean == false)
            nameStreamReader.Close();
            nameStreamWriter.Close();

        this.Close();
    }

我试图做的是在顶部设置 bool isDirtyBoolean,然后当 Stream Reader 或 Writer 关闭时,将 bool 值设置为 true,所以当我退出应用程序时,如果它仍然设置为 false,它无论如何都会关闭它们。

但这不起作用,因为 isDirtyBoolean 值是那些私有的 void 按钮,我无法访问它们。

4

3 回答 3

2

为了更好地跟踪一般情况,请以打开它们的相同方法关闭您的阅读器和编写器,而不是让它们在应用程序期间存在。

如果您使用using它们,它们将被自动处理(并关闭)。例子:

读者:

using (StreamReader reader = new StreamReader(openFileDialog1.FileName)) {

    // do your stuff...

} // automatic close.

你可以对你的 writer 实例做同样的事情。

或者,如果您同时阅读和写作,您可以同时打开并在最后自动关闭,如下所示:

using (StreamReader reader = new StreamReader(openFileDialog1.FileName)) {
    using (StreamWriter writer = new StreamWriter(path_to_other_file)) {

        // now you're reading and writing

        // DO YOUR STUFF

    } // closes writer.
} // closes reader.

之所以using如此,是因为流读取器和写入器类都实现了IDisposable接口;事实上,任何实现此接口的类都可以使用usingon 语句。


并记住要留意 MSDN 文档中的警告,如下所示:

StreamWriter.Dispose 总是关闭流

StreamWriter.Dispose 始终关闭流 Disposing StreamWriter 会在释放底层流时关闭它,即使您使用显式提供流的构造函数也是如此。有时你想保持流打开,但这个类目前不提供这样做的机制,除了不调用 Dispose,但一定要调用 Flush。

于 2010-10-10T01:00:09.740 回答
1

首先,你可以关闭你的流——尽管我会使用“使用”语句来确保它们被关闭,即使有异常。所以当表单退出时不需要关闭它们。

这也是糟糕的编码:

if (isDirtyBoolean == false)
        nameStreamReader.Close();
        nameStreamWriter.Close();

您的 if 条件仅适用于第一条语句。你需要在 if 后面加上花括号。

于 2010-10-09T21:54:35.590 回答
1

使用“使用”或编写您自己的 try/catch/finally 块并在 finally 中关闭您的流。

我假设这是家庭作业,因此如果您的作业要求您检查流,您将不得不在当前声明它们的方法之外声明您的流。当您在方法中声明某些内容时,它仅在该方法中具有范围所以当前在退出按钮处理程序中的代码甚至不应该编译。

如果您在退出按钮处理程序中缺少括号。

于 2010-10-10T00:14:57.337 回答