0

好的,事情就是这样。每当触发触发器时,我都会将我的日志附加到特定文件夹中。代码工作正常,并且正确附加了它,但是如果我尝试从桌面手动删除文件夹,它会给出“操作无法完成,因为文件夹/文件在另一个程序中打开”;

我想我没有正确处理它,但我不知道我错过了它。我知道它是附加到进程的文件夹,因为我试图删除里面的 .log 文件,它允许我。

 private void LogEvent(string filename,bool AppendTxt,string msg)
    {
        string sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
        msg = sLogFormat + msg;
        // create directory
        if (System.IO.Directory.Exists("C:\\Users\\DT-Npax\\Desktop\\LOGS1") != true)
        {
            Directory.CreateDirectory("C:\\Users\\DT-Npax\\Desktop\\LOGS1");
        }
        string dailyLog = "C:\\Users\\DT-Npax\\Desktop\\LOGS1" + "\\" + filename + ".log";
        FileStream FS = null;
        //write or append txt
        if (!AppendTxt)
        {
            if (File.Exists(dailyLog))
            {
                File.Delete(dailyLog);
            }
            using (FS = File.Create(dailyLog)) { }
            FS.Close();
            StreamWriter TXT_WRITE = new StreamWriter(dailyLog);
            TXT_WRITE.WriteLine(msg);
            TXT_WRITE.Close();
        }
        else
        {
            if (!File.Exists(dailyLog))
            {
                  using (FS = File.Create(dailyLog)) { }
                  FS.Close();
            }
            FileStream FSAppend = new FileStream(dailyLog, FileMode.Append, FileAccess.Write);
            StreamWriter TXT_WRITE = new StreamWriter(FSAppend);
            TXT_WRITE.WriteLine(msg);
            TXT_WRITE.Close();
            FSAppend.Close();
        }
    }
4

2 回答 2

1

您的代码似乎确实正确关闭了文件,但不是以异常安全的方式。

您还有一些不必要的代码(如using (FS = File.Create(dailyLog)) { } FS.Close();)。

最小的修改如下所示:

    else
    {
        //if (!File.Exists(dailyLog))
        //{
        //      using (FS = File.Create(dailyLog)) { }
        //      FS.Close();
        //}

        using (FileStream FSAppend = new FileStream(dailyLog, FileMode.Append, FileAccess.Write))
        using (StreamWriter TXT_WRITE = new StreamWriter(FSAppend))
        {
          TXT_WRITE.WriteLine(msg);
        }
        //TXT_WRITE.Close();
        //FSAppend.Close();
    }

但我会像这样重写整个方法:

private void LogEvent(string filename,bool AppendTxt,string msg)
{
    string sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " 
         + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
    msg = sLogFormat + msg;
    // create directory
    if (System.IO.Directory.Exists("C:\\Users\\DT-Npax\\Desktop\\LOGS1") != true)
    {
        Directory.CreateDirectory("C:\\Users\\DT-Npax\\Desktop\\LOGS1");
    }
    string dailyLog = "C:\\Users\\DT-Npax\\Desktop\\LOGS1" + "\\" + filename + ".log";

    if (AppendText)
       System.IO.File.AppendAllText(dailylog, msg);
    else 
       System.IO.File.WriteAllText(dailylog, msg);
}

无需预先创建或删除文件。

于 2013-09-12T10:11:29.230 回答
0

将流包装在 using 块中,因为它们实现了IDisposable.

我必须说这段代码有点奇怪......

using (FS = File.Create(dailyLog)) { }
FS.Close();
StreamWriter TXT_WRITE = new StreamWriter(dailyLog);
TXT_WRITE.WriteLine(msg);
TXT_WRITE.Close();

不应该是这样的:

using (FileStream FS = File.Create(dailyLog)) 
{ 
     using(StreamWriter TXT_WRITE = new StreamWriter(dailyLog))
     {
           TXT_WRITE.WriteLine(msg);
     }
}
于 2013-09-12T10:11:42.270 回答