用C#写了一个静态方法,将日志写入文件系统,可是有时候报:文件正被另一个进程使用
源文件如下:
/// <summary>
/// 生成系统日志。
/// data: 2005-10-07
/// description: 由于在写入数据的时候报IO错误,因此暂时停止日志写入
/// author: chenlihua
/// </summary>
/// <param name="Description">所记录日志描述。</param>
/// <param name="Content">所记录日志内容。</param>
public static void CreateSqlLog(string Description,string Content)
{
/*
string strTmp ="";
string FilePath = @"e:\syslog\sells_log\att_log\";
strTmp = DateTime.Now.Year.ToString()+"\\" +DateTime.Now.Month.ToString()+"\\"+DateTime.Now.Day.ToString()+"\\";
FilePath += strTmp;
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}// 文件夹操作完成。
FilePath += "Att_log_";
FilePath += DateTime.Now.Year.ToString()+"_"+DateTime.Now.Month.ToString()+"_"+DateTime.Now.Day.ToString() + "_"+DateTime.Now.Hour.ToString()+".txt";
StreamWriter sw;
if (!File.Exists(FilePath))
{
sw = File.CreateText(FilePath);
}
else
{
sw = File.AppendText(FilePath);
}
sw.WriteLine("[system time]: "+DateTime.Now.ToString());
sw.WriteLine("---------------------------------------------------------------------------------------");
sw.WriteLine(Description.ToString() + ":" + Content.ToString());
sw.WriteLine();
sw.Close();
*/
}
系统出错提示如下:
调用 exeQuery 接口时出现异常:System.IO.IOException: 该进程无法访问文件“e:\syslog\sells_log\att_log\2005\10\7\Att_log_2005_10_7_15.txt”,因为该文件正由另一进程使用。
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append)
at System.IO.File.AppendText(String path)
at attGateWay.AttAppend.CreateSqlLog(String Description, String Content)
at attGateWay.AttOp.exeQuery(String fromStrIn, String whereStrIn, Int32 SearchType, Boolean IsGetCount, String strRelation, Boolean IsCache, String gh, Int32& ResultCount)
请问此问题如何解决?我在代码中,将StreamWriter对象Close了呀。谢谢!