问题描述:

So i am creating a C# notepad, i am almost done, but theres is one last problem:

I cant save the file. When I open a file, modify it, and try to save it, it gives me an error saying the file is already in use by a process. I believe that this process is started by my open file method, but im not sure. I do have a method to save that open a save file dialog, but I want one that needs no dialog, just a quick cntrl n to save it, you may understand what i mean.

My open file method

 private void openItem_Click(object sender, EventArgs e)

{

Stream myStream;

OpenFileDialog openFileDialog1 = new OpenFileDialog();

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

if ((myStream = openFileDialog1.OpenFile()) != null)

{

string srtfilename = openFileDialog1.FileName;

string filetext = File.ReadAllText(srtfilename);

GetRichTextBox().Text = filetext;

tabControl1.SelectedTab.Text = Path.GetFileName(openFileDialog1.FileName);

GlobalPath = openFileDialog1.FileName;

}

openFileDialog1.Dispose();

}

My save file method WITH dialog, this one works, but if i select the file that I am already using, it crashes.

 private void saveAsItem_Click(object sender, EventArgs e)

{

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

{

using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.Create))

using (StreamWriter sw = new StreamWriter(s))

{ sw.Write(GetRichTextBox().Text); }

tabControl1.SelectedTab.Text = Path.GetFileName(saveFileDialog1.FileName);

saveFileDialog1.Dispose();

}

}

My quick save file method, the one im having problem with. This is what i have tried so far.

 private void saveToolStripMenuItem_Click(object sender, EventArgs e)

{

string texto, nome, local;

nome = tabControl1.SelectedTab.Text;

texto = GetRichTextBox().Text;

tabControl1.TabPages.Remove(tabControl1.SelectedTab);

NewText();

GetRichTextBox().Text = texto;

tabControl1.SelectedTab.Text = nome;

local = nome + ".txt";

using (StreamWriter sw = new StreamWriter(GlobalPath, true))

{ sw.Write(texto); }

// tabControl1.SelectedTab.Text = Path.GetFileName(saveFileDialog1.FileName);

}

网友答案:

Your method for opening and reading a file is overly complicated, and I think it's the source of your problem. You're leaving a Stream object un-disposed. Anything that implements IDisposable should be wrapped in a using statement or explicitly disposed.

Here's a better implementation of your open file method:

private void openItem_Click(object sender, EventArgs e)
{
    using (var openFileDialog1 = new OpenFileDialog())
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try {
                string srtfilename = openFileDialog1.FileName;
                string filetext = File.ReadAllText(srtfilename);
                GetRichTextBox().Text = filetext;

                tabControl1.SelectedTab.Text = Path.GetFileName(openFileDialog1.FileName);

                GlobalPath = openFileDialog1.FileName;
            }
            catch (Exception ex) 
            {
                // you may wish to log the entire exception including stack trace here
                MessageBox.Show(ex.Message);
            }
        }
    }
}

You don't need the stream just to check if a file exists. And now you aren't leaving any undisposed resources that would hold the file open.

You could also use if (File.Exists(strfilename)) to check if the file exists, or just don't do the check, since the FileOpenDialog will actually warn the user by default (I think), if the file doesn't exist.

You should stick with File.ReadAllText and File.WriteAllText: there's no need to be messing with Stream objects directly for a simple text editor, and it's cleaner and leaves you with simpler code.

相关阅读:
Top