如何解决Streamwriter 错误写在它说它无法访问的文件中
我在 C# 控制台应用程序中有一个简单的例程,用于写入日志文件条目。今天,日志中出现了一条奇怪的消息。
The process cannot access the file 'C:\Development-IPL\Logfiles\LogFile20212.txt' because it is being used by another process.
这很奇怪 - 消息写入了它说无法访问的文件中。
代码如下:
public static void WriteLog(string message)
{
var requiredPath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"..\\..\\"));
// use Logfileyyyyww where yyyy is year & ww is week number
DateTime dateTime = DateTime.Now;
int year = dateTime.Year;
int weekno = WeekNo.GetIso8601WeekOfYear(dateTime);
string filePath = requiredPath + "\\Logfiles\\LogFile" + year.ToString() + weekno.ToString() + ".txt";
using (StreamWriter writer = new StreamWriter(filePath,true))
{
if (message != null)
{
writer.WriteLine("Date : " + DateTime.Now.ToString() + ": " + message);
}
}
}
我原以为 Using 语句会确保每次使用此资源时都将其关闭和处理。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。