在记事本程序在C#中“保存文件”的正常运作(Proper functioning of 'S

2019-10-18 09:18发布

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的记事本应用程序,然后打开一个文件,改变它的内容,并保存它,它只是被保存,而无需打开保存文件对话框。 但是,在记事本程序,我创建上面保存文件对话框上单击“保存”保存更改文件的内容后,将打开。 尽管相同的文件名出现在保存文件对话框,但是,点击“保存”,它提供了消息“相同的文件名已经存在。是否要取代它?”。 这就是我想删除并作出改变的内容直接保存到打开的文件,而无需打开保存文件对话框的内容。

Answer 1:

设置sfd.OverwritePrompt =虚假的建设时间后的ShowDialog之前抑制覆盖警告。



Answer 2:

你想有两个选择保存:A“另存为...”按钮和“保存”按钮。 您可以创建一个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语句,并处理任何异常。



Answer 3:

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



文章来源: Proper functioning of 'Save file' in Notepad program in C#