【问题标题】:C# Overwrite text file - nothing is written to fileC# 覆盖文本文件 - 没有任何内容写入文件
【发布时间】:2017-07-13 20:18:54
【问题描述】:

我有两种方法:saveFile 和 saveAsToFile。第一个应该覆盖当前现有文件的内容。而第二个应该保存为新文件(如果当前文件不存在或用户只想制作副本。)

当我使用saveAsToFile 方法时,它每次都有效。当我使用saveFile 方法时,它不会写任何东西。 (不过,我确实看到了“已保存!”消息框。)

这是我的方法:

    public void saveFile(string[] inData, string inDataTitle) 
    {
        //This method saves the file
        SaveFileDialog savefile;
        string trueFileName;

        if (isStrArrayNotEmpty(inData)) {
            //Only attempt to save the file if there is anything written in the textArea

            if (f.getDocumentSavedStatus()) {
                if (f.getDocumentChangedStatus()) {
                    savefile = new SaveFileDialog();

                    if (inDataTitle.EndsWith("*")) {
                        //Remove the asterisk from the document name

                        savefile.FileName = inDataTitle.Substring(0, inDataTitle.Length - 1);
                    }
                    else {
                        savefile.FileName = inDataTitle;
                    }

                    StreamWriter sw = new StreamWriter(savefile.FileName, false);
                    foreach (string line in inData) {
                        sw.WriteLine(line);
                    }
                    sw.Flush();
                    if (sw.BaseStream != null)
                        sw.BaseStream.Flush();
                    sw.Close();

                    /*
                    using (StreamWriter sw = new StreamWriter(savefile.FileName, false))
                        foreach (string line in inData) {
                            sw.WriteLine(line);
                        }
                    */

                    f.setDocumentName(string.Empty);
                    trueFileName = Path.GetFileName(savefile.FileName);
                    f.setDocumentName(trueFileName);
                    f.setDocumentSavedStatus(true);
                    f.setDocumentChangedStatus(false);      //Changes saved, status updated

                    MessageBox.Show("Saved!");
                }
            }
            else {
                //If the document hasn't been saved before, send the values to the 'Save As' method

                saveAsToFile(inData, inDataTitle);
            }
        }
    }

    public void saveAsToFile(string[] inData, string inDataTitle) 
    {
        //This method checks if there is anything written in the texArea,
        //if so it prompts the user to save the file to disk (Save As)

        SaveFileDialog savefile;
        string trueFileName;

        if (isStrArrayNotEmpty(inData)) {
            //Only attempt to save the file if there is anything written in the textArea

            savefile = new SaveFileDialog();

            if (inDataTitle.EndsWith("*")) {
                //Remove the asterisk from the document name

                savefile.FileName = inDataTitle.Substring(0, inDataTitle.Length - 1);
            }
            else {
                savefile.FileName = inDataTitle;
            }

            savefile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

            if (savefile.ShowDialog() == DialogResult.OK) {
                StreamWriter sw = new StreamWriter(savefile.FileName, false);
                foreach (string line in inData) {
                    sw.WriteLine(line);
                }
                sw.Flush();
                if (sw.BaseStream != null)
                    sw.BaseStream.Flush();
                sw.Close();


                /*
                using (StreamWriter sw = new StreamWriter(savefile.FileName))
                    foreach (string line in inData) {
                        sw.WriteLine(line);
                    }
                */

                f.setDocumentName(string.Empty);
                trueFileName = Path.GetFileName(savefile.FileName);
                f.setDocumentName(trueFileName);
                f.setDocumentSavedStatus(true);
                f.setDocumentChangedStatus(false);      //Changes saved, status updated
            }
        }
    }

从代码中的 cmets 可以看出;我尝试使用using,然后尝试“手动刷新”streamwriter,但没有任何效果。

使用saveAsToFile 每次都有效。它按预期覆盖文本文件,没有问题。虽然saveFile 没有向文件写入任何内容。 (保持不变。)

我尝试通过使用MessageBox.Show 在适当的位置打印savefile.Filename 和line 的值来查找saveFile 中的错误——它们都按预期工作,但没有任何内容写入实际的文本文件。

isStrArrayNotEmpty 如果字符串数组不包含任何空格,则返回 true。 getDocumentSavedStatus 返回 boolean 的值,它告诉文件是否之前已保存(存在/不存在) getDocumentChangedStatus 返回 boolean 的值,它告诉文件是否已被修改(文件名末尾的星号,表示如果用户关闭应用程序将丢失工作。)

【问题讨论】:

  • 我建议使用 Visual Studio 调试器。并花一些时间分析您的代码。

标签: c# streamwriter system.io.file


【解决方案1】:

inDataTitle 参数是否包含您要保存的文件名的路径?如果不是,它可能会保存到同名但在不同文件夹中的文件中。

在你的行之后:-

StreamWriter sw = new StreamWriter(savefile.FileName, false);

添加行:-

MessageBox.Show(((FileStream)(sw.BaseStream)).Name);

它会告诉你文件的保存位置。

【讨论】:

  • 你是对的。路径不见了!我现在将尝试重新编写我的代码(我目前正在尝试将这两种方法合并为一种。)谢谢。 =)
【解决方案2】:

尝试替换

StreamWriter sw = new StreamWriter(savefile.FileName, false);
   foreach (string line in inData) {
        sw.WriteLine(line);
    }
    sw.Flush();
    if (sw.BaseStream != null)
        sw.BaseStream.Flush();
    sw.Close();

与

File.WriteAllLines(saveFile.Filename, inData);

【讨论】:

  • 我试过了,可惜没用。结果和以前一样。 (使用您在方法“saveAsToFile”中建议的行确实有效,但在方法“saveFile”中无效。)
  • 您是否在调试器中单步执行了代码?你知道 savefile.Filename 是否真的有你期望的值吗?
  • 我真的不知道这个混蛋是如何工作的。在运行应用程序时,我使用 MessageBox.Show 打印变量的值。编辑:是的,文件名是我所期望的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 2012-05-15
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多