C# 我试图用不同的方法访问同一个日志文件,但遇到了错误
我试图创建一个日志文件,并写入该文件,我使用不同的方法使用同一个文件,我得到一个错误。如果我做错了,请纠正我C# 我试图用不同的方法访问同一个日志文件,但遇到了错误,c#,winforms,C#,Winforms,我试图创建一个日志文件,并写入该文件,我使用不同的方法使用同一个文件,我得到一个错误。如果我做错了,请纠正我 foreach (var corporationObj in corporations) { // Log which corporation it is currenlty doing _keepItDry.Log("Started Data Summarisation for corporation: @" + corporationObj.Description,
foreach (var corporationObj in corporations)
{
// Log which corporation it is currenlty doing
_keepItDry.Log("Started Data Summarisation for corporation: @" + corporationObj.Description, w);
// Pass the coporationId and call method to DoProductStatsForCorporation and also pass the log file to continue our logging.
w.Flush();
// w.Close();
w.Dispose();
DoProductStatsForCorporation(corporationObj.Id, path);
}
下面是循环中调用的方法的列表:
private void DoProductStatsForCorporation(int corporationId, string logFilePath)
{
var corporationManufacturerRepository = new CorporationManufacturerRepository();
var manufacturerRepository = new ManufacturerRepository();
// Get brand sfor the coporationId passes
var corporationManufacturers = corporationManufacturerRepository.GetAllManufacturersForCorporation(corporationId);
//check before process is used by another object
var fileInfo = new FileInfo(logFilePath);
IsFileLocked(fileInfo);
using (StreamWriter w = File.AppendText(logFilePath))
{
// If brands are existing for the corporation proceed.
if (corporationManufacturers.Any())
{
// loop through each brand to processs
foreach (var corporationManufacturer in corporationManufacturers)
{
#region ProductStats
// get the manufacturer row so that we can know more details about the manufacturer
var manufacturer = manufacturerRepository.GetManufacturer(corporationManufacturer.ManufacturerId);
// Get the countries for the manufacturer
_keepItDry.Log("Started Doing data summarisation for Brand: @ " + manufacturer.Description, w);
// this is common method so extracted to reuse it.
// we need to clos ethe file as we are sending to another function.
w.Flush();
w.Dispose();
DoProductStatsForBrand(manufacturer, logFilePath);
#endregion ProductStats
_keepItDry.AddTolistBox(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ": Ended Summarizing data for " + manufacturer.Description, _listBoxLog);
}
}
else
{
_keepItDry.Log(" No Brands are there for the selected Corporation Please check if Brands are mapped for this Corporation ? : @ ", w);
_keepItDry.AddTolistBox(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + " : No Brands are there for the selected Corporation Please check if Brands are mapped for this Corporation ", _listBoxLog);
}
// w.Flush();
// w.Dispose();
}
// GC.Collect();
}
来取下一个公司Id后,我得到的是“当前文件已关闭”。首先是
using (StreamWriter w ...
w.Flush();
w.Dispose();
那么你的内心有
`foreach` loop
里面你有
using (StreamWriter w ...
w.Flush();
w.Dispose();
你刚刚处理了你的物体-它将如何工作?
如果您有2个公司制造商,您将执行一个循环,然后出现错误
使用
是您的处理机制。您只需要在内部添加一个标志来验证块是否完成良好。您可以使用StreamWriter
来编写文件。
写入文件后,请处理该文件
streamWriter.Flush();
streamWriter.Close();
Close()
也调用Dispose()
方法。您收到了什么异常?@M.Babcock进程无法访问文件“d:\DataSummarisationLogfiles\2013-09-17\DataSummarisation2013-09-17 01-41-52-AM”,因为它正被另一个进程使用。如果我这样做,则关闭()然后,当涉及到第二个循环时,我发现它说无法向封闭的TextWriter写入。问题可能是文件没有被打开以进行共享写入,但您的代码有点混乱,无法确定。