我怎样才能保存这个文件?

How can I save this file?

所以我正在创建一个 C# 记事本,我快完成了,但还有最后一个问题: 我无法保存文件。当我打开一个文件、修改它并尝试保存它时,它给我一个错误,指出该文件已被进程使用。我相信这个过程是由我的打开文件方法启动的,但我不确定。我确实有一种打开保存文件对话框的保存方法,但我想要一个不需要对话框的方法,只需快速 cntrl n 即可保存它,您可能会明白我的意思。

我的打开文件方法

 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();
        }

我的带有对话框的保存文件方法,这个有效,但是如果我 select 我已经在使用的文件,它会崩溃。

 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();
        }
    }

我的 quick 保存文件的方法,我用的那个有问题。这是我到目前为止尝试过的。

 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);
    }

您打开和读取文件的方法过于复杂,我认为这是您问题的根源。您未处理 Stream 对象。任何实现 IDisposable 的东西都应该包含在 using 语句中或显式处理。

下面是打开文件方法的更好实现:

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);
            }
        }
    }
}

您不需要流只是为了检查文件是否存在。现在您不会留下任何未处理的资源来保持文件打开。

您也可以使用 if (File.Exists(strfilename)) 检查文件是否存在,或者干脆不检查,因为如果文件不存在,FileOpenDialog 实际上会默认警告用户(我认为)不存在。

你应该坚持使用 File.ReadAllTextFile.WriteAllText:没有必要为了一个简单的文本编辑器直接弄乱 Stream 对象,它更干净,让你的代码更简单.