正确处理文件对象

时间:2013-09-12 10:04:50

标签: c# service directory filestream

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

我想我没有正确处理它,但我不知道我错过了哪里。我知道它附加到进程的文件夹,因为我试图删除里面的.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();
        }
    }

2 个答案:

答案 0 :(得分: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);
}

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

答案 1 :(得分: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);
     }
}