尝试在不使用SaveFileDialog

时间:2016-03-10 08:29:29

标签: c# text-editor savefiledialog

我正在创建一个文本编辑器,我被困在SaveFileDialog窗口打开 并要求打开当前文件。

我已经在SO上看到了所有类似问题,但是没有人能够帮助我。我甚至尝试过这个问题的代码:"没有对话框保存文件" Saving file without dialog

我遇到了FileName有问题的程序。

这是我目前的代码

namespace Text_Editor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();

            open.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
            open.Title = "Open File";
            open.FileName = "";

            if (open.ShowDialog() == DialogResult.OK)
            {
                this.Text = string.Format("{0}", Path.GetFileNameWithoutExtension(open.FileName));
                StreamReader reader = new StreamReader(open.FileName);
                richTextBox1.Text = reader.ReadToEnd();
                reader.Close();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();

            save.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
            save.Title = "Save File";

            save.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            if (save.ShowDialog() == DialogResult.OK)
            {
                StreamWriter writer = new StreamWriter(save.FileName);
                writer.Write(richTextBox1.Text);
                writer.Close();
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saving = new SaveFileDialog();

            saving.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            saving.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
            saving.Title = "Save As";
            saving.FileName = "Untitled";

            if (saving.ShowDialog() == DialogResult.OK)
            {
                StreamWriter writing = new StreamWriter(saving.FileName);
                writing.Write(richTextBox1.Text);
                writing.Close();
            }
        }

    }
}

所以我的问题是如何修改我的代码以便我可以保存当前打开的文件而不必每次都打开SaveFileDialog框?

我知道这与我调用.ShowDialog这一事实有关,但我不知道如何修改它。

4 个答案:

答案 0 :(得分:2)

您必须存储已保存文件的事实,例如将文件名存储在Form类的member variable中。然后使用if检查您是否已保存文件,然后使用SaveFileDialog显示ShowDialog()(如果您还没有)或不显示,请继续保存到已定义的文件名(存储在您的成员变量中)。

试试看,请执行以下操作:

  • 定义string成员变量,将其称为_fileName(您班级中的private string _fileName;
  • saveToolStripMenuItem_Click方法中,检查它是nullif (null == _fileName)
  • 如果是null,请像以前一样继续(显示对话框),获取文件名后,将其存储在您的成员变量中
  • 重构文件编写代码,以便从文件对话框中获取文件名(如前所述),或从成员变量_fileName
  • 获取文件名

玩得开心,C#是一门很棒的语言。

答案 1 :(得分:2)

例如,在类中创建一个新的字符串变量     string filename = string.empty

然后

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
  if(string.IsNullOrEmpty(filename)) {
 //Show Save filedialog
    SaveFileDialog save = new SaveFileDialog();

    save.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
    save.Title = "Save File";

    save.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    if (save.ShowDialog() == DialogResult.OK)
    {
        filename = save.FileName;

    }
   }
   StreamWriter writer = new StreamWriter(filename);
        writer.Write(richTextBox1.Text);
        writer.Close();
}

SaveFileDialog现在仅在fileName为null或为空

时打开

答案 2 :(得分:2)

打开文件时,将FileName保存在表单级变量或属性中。

现在,在保存文件时,您可以使用此FileName,而不是从FileOpenDialog获取它。

首先声明一个变量以保存表单级别的文件名

// declare at form level
private string FileName = string.Empty;

打开文件时,将FileName保存在此变量中

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();

    open.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
    open.Title = "Open File";
    open.FileName = "";

    if (open.ShowDialog() == DialogResult.OK)
    {
        // save the opened FileName in our variable
        this.FileName = open.FileName;
        this.Text = string.Format("{0}", Path.GetFileNameWithoutExtension(open.FileName));
        StreamReader reader = new StreamReader(open.FileName);
        richTextBox1.Text = reader.ReadToEnd();
        reader.Close();
    }
}

执行SaveAs操作时,请更新此变量

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
    SaveFileDialog saving = new SaveFileDialog();

    saving.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    saving.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
    saving.Title = "Save As";
    saving.FileName = "Untitled";

    if (saving.ShowDialog() == DialogResult.OK)
    {
        // save the new FileName in our variable
        this.FileName = saving.FileName;
        StreamWriter writing = new StreamWriter(saving.FileName);
        writing.Write(richTextBox1.Text);
        writing.Close();
    }
}

然后可以像这样修改保存功能:

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(this.FileName))
    {
        // call SaveAs 
        saveAsToolStripMenuItem_Click(sender, e);
    } else {
        // we already have the filename. we overwrite that file.
        StreamWriter writer = new StreamWriter(this.FileName);
        writer.Write(richTextBox1.Text);
        writer.Close();
    }
}

在New(和Close)函数中,您应该清除此变量

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
    // clear the FileName
    this.FileName = string.Empty;
    richTextBox1.Clear();
}

答案 3 :(得分:1)

首先,来自saveAsToolStripMenuItem_Click提取方法:如果您想要添加弹出菜单速度按钮,该怎么办?然后实施

  public partial class Form1: Form {
    // File name to save text to  
    private String m_FileName = "";

    private Boolean SaveText(Boolean showDialog) {
      // If file name is not assigned or dialog explictly required
      if (String.IsNullOrEmpty(m_FileName) || showDialog) {
        // Wrap IDisposable into using
        using (SaveFileDialog dlg = new SaveFileDialog()) {
          dlg.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
          dlg.Title = "Save File";
          dlg.FileName = m_FileName;  

          if (dlg.ShowDialog() != DialogResult.OK) 
            return false;

          m_FileName = dlg.FileName;  
        } 
      }

      File.WriteAllText(m_FileName, richTextBox1.Text);

      this.Text = Path.GetFileNameWithoutExtension(m_FileName);

      return true;
    }

    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
      // SaveAs: always show the dialog 
      SaveText(true);
    }

    private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
      // Save: show the dialog when required only
      SaveText(false);
    }
    ...
  }