简体   繁体   中英

C# The process can't access the file because it is being used by another process (File Dialog)

I have a rich text box that is being updated with log information. There is a button to save the log output to a file. When I use the code below to try to save the output to a file, I receive "The process can't access the file because it is being used by another process" exception. I am not sure why I am receiving this exception. It happens on new files that I create in the dialog. It happens on any file I try to save the information to.

private void saveLog_Click(object sender, EventArgs e)
{
            OnFileDialogOpen(this, new EventArgs());
            // Displays a SaveFileDialog so the user can save the Image
            // assigned to Button2.
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Text File|*.txt|Log File|*.log";
            saveFileDialog1.Title = "Save Log File";
            saveFileDialog1.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if (saveFileDialog1.FileName != "")
            {
                // Saves the Image via a FileStream created by the OpenFile method.
                System.IO.FileStream fs =
                   (System.IO.FileStream)saveFileDialog1.OpenFile();
                // Saves the Image in the appropriate ImageFormat based upon the
                // File type selected in the dialog box.
                // NOTE that the FilterIndex property is one-based.
                switch (saveFileDialog1.FilterIndex)
                {
                    case 1:

                        try
                        {
                            this.logWindow.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }


                        break;
                    case 2:

                        try
                        {
                            this.logWindow.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }

                        break;
                }

                fs.Close();
                OnFileDialogClose(this, new EventArgs());
            }
}

It seems the same file is opened twice. First you create it using:

System.IO.FileStream fs =
                   (System.IO.FileStream)saveFileDialog1.OpenFile();

Then you pass the same file name to this.logWindow.SaveFile , which presumably opens the file with the given name and saves data to it.

I guess the first call is unnecessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM