C#中记事本程序中“保存文件”的正常运行

时间:2013-07-15 19:02:28

标签: c# file save notepad

public partial class Form1 : Form
{
    SaveFileDialog sfd = new SaveFileDialog();
    OpenFileDialog ofd = new OpenFileDialog();
    public string contents = string.Empty;
    public Form1()
    {
        InitializeComponent();
        this.Text = "Untitled";
    }

    private void newToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (richTextBox1.Text != contents)
        {
            DialogResult dr = MessageBox.Show("Do You want to save the changes made to " + this.Text, "Save", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                sfd.Title = "Save";
                if (SaveFile() == 0)
                    return;
                else
                {
                    richTextBox1.Text = "";
                    this.Text = "Untitled";
                }
                contents = "";
            }
            else if (dr == DialogResult.No)
            {
                richTextBox1.Text = "";
                this.Text = "Untitled";
                contents = "";
            }
            else
            {
                richTextBox1.Focus();
            }
        }
        else
        {
            this.Text = "Untitled";
            richTextBox1.Text = "";
            contents = "";
        }

    }
    private int SaveFile()
    {
        sfd.Filter = "Text Documents|*.txt";
        sfd.DefaultExt = "txt";
        if (sfd.ShowDialog() == DialogResult.Cancel)
        {
            richTextBox1.Focus();
            return 0;
        }
        else
        {
            contents = richTextBox1.Text;
            if (this.Text == "Untitled")
                richTextBox1.SaveFile(sfd.FileName,RichTextBoxStreamType.PlainText);
            else
            {
                sfd.FileName = this.Text;
                richTextBox1.SaveFile(sfd.FileName,RichTextBoxStreamType.PlainText);
            }
            this.Text = sfd.FileName;
            return 1;
        }


    }
    private void OpenFile()
    {
        ofd.Filter = "Text Documents|*.txt";
        if (ofd.ShowDialog() == DialogResult.Cancel)
            richTextBox1.Focus();
        else
        {
            richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
            this.Text = ofd.FileName;
            contents = richTextBox1.Text;
        }
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (richTextBox1.Text != contents)
        {
            DialogResult dr = MessageBox.Show("Do You want to save the changes made to " + this.Text, "Save", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                SaveFile();
                OpenFile();
            }
            else if (dr == DialogResult.No)
            {
                OpenFile();
            }
            else
            {
                richTextBox1.Focus();
            }
        }
        else
            OpenFile();
    }

    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SaveFile();
    }

    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        sfd.Filter = "Text Documents|*.txt";
        sfd.DefaultExt = "txt";
        if (sfd.ShowDialog() == DialogResult.Cancel)
        {
            richTextBox1.Focus();
        }
        else
        {
            contents = richTextBox1.Text;
            richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);

            this.Text = sfd.FileName;
        }            
    }

当我们打开Windows记事本应用程序然后打开一个文件,更改内容并保存它,它只是保存而不打开“保存文件”对话框。但是在我上面创建的记事本程序中,在更改保存文件的内容后单击“保存”时会打开“保存文件”对话框。虽然保存文件对话框中显示相同的文件名,但单击“保存”时会显示“同一文件名已存在。是​​否要替换它?”。这就是我要删除的内容,并将更改的内容直接保存到打开的文件中,而无需打开保存文件对话框。

3 个答案:

答案 0 :(得分:1)

在构造之后和ShowDialog之前设置sfd.OverwritePrompt = false以禁止覆盖警告。

答案 1 :(得分:1)

您想要保存两个选项:“另存为...”按钮和“保存”按钮。您可以创建string来保存打开文件的路径。如果用户在保存文件时指定新位置,则位置也可能会改变。如果用户没有打开文件,“另存为...”按钮将打开常规的“保存文件”对话框。一旦用户指定了文档的位置,您就可以将文件路径保存到该字符串,并使用“StreamWriter”保存它而不使用对话框:

...
using System.IO;
...

public partial class Form1 : Form
{
    SaveFileDialog sfd = new SaveFileDialog();
    OpenFileDialog ofd = new OpenFileDialog();
    public string contents = string.Empty;

    //string to hold file location
    string currentFileLoc;


    public Form1()
    {
        InitializeComponent();
        this.Text = "Untitled";
    }

    private void newToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (richTextBox1.Text != contents)
        {
            DialogResult dr = MessageBox.Show("Do You want to save the changes made to " + this.Text, "Save", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                sfd.Title = "Save";
                if (SaveFile() == 0)
                    return;
                else
                {
                    richTextBox1.Text = "";
                    this.Text = "Untitled";
                }
                contents = "";
            }
            else if (dr == DialogResult.No)
            {
                richTextBox1.Text = "";
                this.Text = "Untitled";
                contents = "";
            }
            else
            {
                richTextBox1.Focus();
            }
        }
        else
        {
            this.Text = "Untitled";
            richTextBox1.Text = "";
            contents = "";
        }

    }
    private int SaveFile()
    {
        sfd.Filter = "Text Documents|*.txt";
        sfd.DefaultExt = "txt";
        if (sfd.ShowDialog() == DialogResult.Cancel)
        {
            richTextBox1.Focus();
            return 0;
        }
        else
        {
            contents = richTextBox1.Text;
            if (this.Text == "Untitled")
                richTextBox1.SaveFile(sfd.FileName,RichTextBoxStreamType.PlainText);
            else
            {
                sfd.FileName = this.Text;
                richTextBox1.SaveFile(sfd.FileName,RichTextBoxStreamType.PlainText);
            }
            this.Text = sfd.FileName;
            //
            currentFileLoc = sfd.FileName;
            return 1;
        }


    }
    private void OpenFile()
    {
        ofd.Filter = "Text Documents|*.txt";
        if (ofd.ShowDialog() == DialogResult.Cancel)
            richTextBox1.Focus();
        else
        {
            richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
            this.Text = ofd.FileName;
            contents = richTextBox1.Text;
        }

        currentFileLoc = ofd.FileName;
        this.Text = currentFileLoc;
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (richTextBox1.Text != contents)
        {
            DialogResult dr = MessageBox.Show("Do You want to save the changes made to " + this.Text, "Save", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                SaveFile();
                OpenFile();
            }
            else if (dr == DialogResult.No)
            {
                OpenFile();
            }
            else
            {
                richTextBox1.Focus();
            }
        }
        else
            OpenFile();
    }

    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Save();
    }

    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SaveFile();            
    }

    //new method
    private void Save()
    {
        if (currentFileLoc != null)
        {
            using (StreamWriter writer = new StreamWriter(currentFileLoc))
            {
                writer.WriteLine(richTextBox1.Text);
            }
        }

        else
          saveFile();
     }
 }

我建议您将using(...){ }块括在try/catch语句中并处理任何异常。

答案 2 :(得分:0)

您需要做的是保存输入的文件名,然后,当按下保存选项时,检查以前输入的文件名。如果您有,请跳过显示对话框并执行保存代码。