C#记事本程序中“保存文件"的正常运行 [英] Proper functioning of 'Save file' in Notepad program in C#

查看:86
本文介绍了C#记事本程序中“保存文件"的正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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记事本应用程序然后打开文件,更改其内容并保存时,只需打开它即可,而无需打开保存文件"对话框.但是在更改文件内容后,单击保存"时,在保存文件"对话框上方创建的记事本程序中打开.尽管相同的文件名出现在保存文件"对话框中,但是在单击保存"时会显示一条消息该文件名已经存在.您要替换它吗?".这就是我要删除的内容,并使更改后的内容直接保存到打开的文件中,而无需打开保存文件"对话框.

When we open Windows notepad application then open a file, changes it contents and save it, it simply gets saved without opening the Save File dialog. But in the notepad program I have created above the Save File dialog opens on clicking 'Save' after changing the contents of saved file. Although the same file name appears in the save file dialog but on clicking 'Save' it gives a message "The same file name already exists. Do you want to replace it?". That is what I want to remove and make the changed contents saved directly to the opened file without opening the save file dialog.

推荐答案

您要保存两个选项:另存为.."按钮和保存"按钮.您可以创建一个string来保存打开的文件的路径.如果用户在保存文件时指定了新位置,则该位置也可能会更改.如果用户未打开文件,则另存为..."按钮将打开常规的保存文件"对话框.用户指定了文档的位置后,您可以将文件路径保存到该字符串,并使用"StreamWriter"将其保存,而无需对话框:

You want to have two choices to save: A 'Save As..' button and a 'Save' button. You can create a string to hold the path of the opened file. The location may also change if the user specifies a new location when they save the file. If the user did not open the file, the 'Save As...' button would open the regular Save File Dialog. Once the user specifies the location of their document, you can save the file path to that string and use a `StreamWriter' to save it without the dialog:

...
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语句中,并处理所有异常.

I suggest you also enclose the using(...){ } block in a try/catch statement and handle any exceptions.

这篇关于C#记事本程序中“保存文件"的正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆