【发布时间】: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