/// <summary>

/// Writes log file.

/// </summary>

/// <param name="filePath">file path</param>

/// <param name="content">file content</param>

/// <param name="append">whether append to end of the file</param>

public static void WriteLogFile(string filePath, string content, bool append)

{

FileInfo fi = new FileInfo(filePath);

 

if (fi.Exists)

{

if (!append)

{

fi.Delete();

}

}

else

{

using (FileStream fs = fi.Create())

{ }

}

 

using (StreamWriter sw = fi.AppendText())

{

sw.WriteLine(content);

}

}

 

其中这一句:

using (FileStream fs = fi.Create())

{ }

一定要用using, 否则FileStream不会被释放,导致后面的文件操作提示:"The process cannot access the file <file name> because it is being used by another process."

posted on 2012-02-03 16:06  今夜太冷  阅读(256)  评论(0)    收藏  举报