1

我收到的完整错误是:

“该进程无法访问文件 'e:\Batch\NW\data_Test\IM_0232\input\RN318301.WM',因为它正被另一个进程使用。>>> 在 IM_0232.BatchModules.BundleSort(String bundleFileName) 在 IM_0232。 BatchModules.ExecuteBatchProcess()"

所涉及的代码如下所示。正在处理的RN318301.WM文件是一个文本文件,其中包含最终将放置在 PDF 文档中的信息。文本文件中引用了许多文档RN318301.WM,每个文档都由一组行表示。从代码中可以看出,RN318301.WM首先解析文本文件以确定其中表示的文档数以及文档中的最大行数。然后使用此信息创建包含所有文档信息的二维数组。RN318301.WM再次解析文本文件以填充二维数组,同时将信息收集到字典中,该字典稍后将在例程中进行排序。

失败发生在下面的最后一行:

File.Delete(_bundlePath + Path.GetFileName(bundleFileName));

这是一个很少发生的零星问题。甚至可以看到它出现在以前没有出现过的特定文本文件中。也就是说,一个特定的文本文件会处理得很好,但是在重新处理时会触发错误。

任何人都可以帮助我们诊断此错误的原因吗?非常感谢...

public void BundleSort(string bundleFileName)
    {

        Dictionary<int, string> memberDict = new Dictionary<int, string>();
        Dictionary<int, string> sortedMemberDict = new Dictionary<int, string>();
        //int EOBPosition = 0;
        int EOBPosition = -1;
        int lineInEOB = 0;
        int eobCount = 0;
        int lineCount = 0;
        int maxLineCount = 0;
        string compareString;
        string EOBLine;
        //@string[][] EOBLineArray;
        string[,] EOBLineArray;

        try
        {
            _batch.TranLog_Write("\tBeginning sort of bundle " + _bundleInfo.BundleName + " to facilitate householding");

            //Read the bundle and create a dictionary of comparison strings with EOB position in the bundle being the key

            StreamReader file = new StreamReader(@_bundlePath + _bundleInfo.BundleName);

            //The next section of code counts CH records as well as the maximum number of CD records in an EOB.  This information is needed for initialization of the 2-dimensional EOBLineArray array.

            while ((EOBLine = file.ReadLine()) != null)
            {
                if (EOBLine.Substring(0, 2) == "CH" || EOBLine.Substring(0, 2) == "CT")
                {
                    if (lineCount == 0)
                        lineCount++;

                    if (lineCount > maxLineCount)
                    {
                        maxLineCount = lineCount;
                    }
                    eobCount++;
                    if (lineCount != 1)
                        lineCount = 0;

                }
                if (EOBLine.Substring(0, 2) == "CD")
                {
                    lineCount++;
                }
            }

            EOBLineArray = new string[eobCount, maxLineCount + 2];


            file = new StreamReader(@_bundlePath + _bundleInfo.BundleName);


                try
                {



                    while ((EOBLine = file.ReadLine()) != null)
                    {

                        if (EOBLine.Substring(0, 2) == "CH")
                        {
                            EOBPosition++;
                            lineInEOB = 0;
                            compareString = EOBLine.Substring(8, 40).Trim() + EOBLine.Substring(49, 49).TrimEnd().TrimStart() + EOBLine.Substring(120, 5).TrimEnd().TrimStart();
                            memberDict.Add(EOBPosition, compareString);

                            EOBLineArray[EOBPosition, lineInEOB] = EOBLine;

                        }
                        else
                        {
                            if (EOBLine.Substring(0, 2) == "CT")
                            {
                                EOBPosition++;
                                EOBLineArray[EOBPosition, lineInEOB] = EOBLine;
                            }
                            else
                            {
                                lineInEOB++;
                                EOBLineArray[EOBPosition, lineInEOB] = EOBLine;

                            }
                        }

                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                _batch.TranLog_Write("\tSending original unsorted bundle to archive");
           if(!(File.Exists(_archiveDir + "\\" +DateTime.Now.ToString("yyyyMMdd")+ Path.GetFileName(bundleFileName) + "_original")))
             {
                 File.Copy(_bundlePath + Path.GetFileName(bundleFileName), _archiveDir + "\\" +DateTime.Now.ToString("yyyyMMdd")+ Path.GetFileName(bundleFileName) + "_original");
             }


                file.Close();
                file.Dispose();
            GC.Collect();

                File.Delete(_bundlePath + Path.GetFileName(bundleFileName));
4

2 回答 2

3

您没有关闭/处置您的StreamReader第一轮,因此文件句柄仍处于打开状态

考虑使用using构造 - 当它超出范围时,这将自动处理对象:

using(var file = new StreamReader(args)) 
{
    // Do stuff
}

// file has now been disposed/closed etc
于 2013-07-08T16:53:18.310 回答
2

您需要为一件事关闭 StreamReaders。

StreamReader file = new StreamReader(@_bundlePath + _bundleInfo.BundleName);

您需要关闭 StreamReader 对象,您可以在 finally 块中执行此操作:

finally {
  file.Close();
}

更好的方法是使用 using 块:

using (StreamReader file = new StreamReader(@_bundlePath + _bundleInfo.BundleName)) {
    ...
}

在我看来,您正在打电话GC.Collect试图强制关闭这些 StreamReader,但这并不能保证它们会按照 MSDN 文档立即关闭:http: //msdn.microsoft.com/en-us/库/xe0c2357.aspx

来自该文档:“所有对象,无论它们在内存中存在多长时间,都将被考虑收集;”

于 2013-07-08T16:52:57.293 回答