“进程无法访问该文件,因为它正由另一个进程使用”

时间:2013-07-08 16:48:33

标签: c#

我收到的完整错误是:

  

“进程无法访问文件'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));

2 个答案:

答案 0 :(得分:3)

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

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

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

// file has now been disposed/closed etc

答案 1 :(得分: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/library/xe0c2357.aspx

从那个文档: “所有物品,无论它们记忆多久,都被视为用于收藏;”