0

ログ ファイルを作成してそのファイルに書き込もうとしていますが、同じファイルを別の方法で使用していますが、エラーが発生しています。私が間違っている場合は、私を修正してください。

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を取得した後、「現在のファイルは閉じられています」というメッセージが表示されます。

4

2 に答える 2

1

まずあなたが持っている

using (StreamWriter w ...

次に、内部にある

`foreach`  loop

あなたが持っている中に

w.Flush();
w.Dispose();

オブジェクトを破棄しました - どのように動作しますか? 2 がcorporationManufacturersある場合、1 つのループに進み、エラーが発生します...

usingあなたの処分メカニズムです。ブロックが正常に完了したことを確認するには、内部にフラグが必要です。

于 2013-09-17T03:39:05.400 に答える
0

StreamWriterファイルの書き込みに使用できます。ファイルを書き込んだ後、ファイルを破棄します。

streamWriter.Flush();
streamWriter.Close();

Close()Dispose()メソッドも呼び出します。

于 2013-09-17T05:57:20.740 に答える