/// <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."
浙公网安备 33010602011771号