-1

I am trying to read multiple cells from a range out of an .xslx-file. I get the error that a process can not access the file because it is being used by another process.

First I had the problem that has been described in this topic: (unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'"). I fixed that problem by running the repair for MSOffice Professional Plus. After I have done that, the errors I am mentioning after the code block started occurring. To test the behavior of them I created a new project which ends up getting the same errors.

I am using following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;

namespace EA_Excel_ReadRange
{
class Program
{
    static void Main(string[] args)
    {

                    Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"PathToXslx");
        Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
        Excel.Range xlRange = xlWorksheet.UsedRange;

        int rowCount = xlRange.Rows.Count;
        int colCount = xlRange.Columns.Count;

        for (int i = 1; i <= rowCount; i++)
        {
            for (int j = 1; j <= colCount; j++)
            {
                if (j == 1)
                    Console.Write("\r\n");

                if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                    Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");
            }
        }

        GC.Collect();
        GC.WaitForPendingFinalizers();

        Marshal.ReleaseComObject(xlRange);
        Marshal.ReleaseComObject(xlWorksheet);

        xlWorkbook.Close();
        Marshal.ReleaseComObject(xlWorkbook);

        xlApp.Quit();
        Marshal.ReleaseComObject(xlApp);

    }
}

}

I can run the code one time, where I get warned if that has not been found, that I should check if it is there, etc. After that one time I get following errors in VS 2019 Community. I checked the taskmanager as well, but there is no such process as it is described in the error messages.

Could not copy "obj\Debug\EA_Excel_ReadRange.exe" to "bin\Debug\EA_Excel_ReadRange.exe". Exceeded retry count of 10. Failed. The file is locked by: "EA_Excel_ReadRange.exe (21944)" EA_Excel_ReadRange

&

Unable to copy file "obj\Debug\EA_Excel_ReadRange.exe" to "bin\Debug\EA_Excel_ReadRange.exe". The process cannot access the file 'bin\Debug\EA_Excel_ReadRange.exe' because it is being used by another process. EA_Excel_ReadRange

Please explain why these errors are occuring and how I can fix them so the code will run. I just recently started programming in C# so I might have missed something in my code that is causing those errors.

Let me know if I need to clarify something.

EDIT The file I am using was downloaded from a Excel online company sheet on Sharepoint on Premises

Liam
  • 27,717
  • 28
  • 128
  • 190
jmehrsam
  • 21
  • 6
  • Is your Excel file open? The error suggests that another program has an exclusive lock on your Excel file, preventing your program from accessing it. – Kei Sep 16 '19 at 12:44
  • On an unrelated noted, I think you generally shouldn't need to manually invoke GC.Collect, GC.WaitForPendingFinalizers, or Marshal.ReleaseComObject – Kei Sep 16 '19 at 12:48
  • 3
    It is a build error and doesn't have anything to do with your code. Use Task Manager, Processes tab to verify that your program is not still running. You next need to temporarily disable your anti-malware product. By far the most common cause, a lot of them are not suitable for use on a programmer's machine since they don't deal well with an executable file appearing from seemingly no-where. Favor the one included with your OS. – Hans Passant Sep 16 '19 at 12:56
  • No, my Excel file is not open. Is there an easy way for me to find out if there is a program that "locks" the Excel? The Excel file was once created in Sharepoint on Premises (will add that to the original post) then I downloaded it for the task I need to complete. I know that there are lots of functions on cells to have a specific behavior. Nevertheless, I do not think that this would be the problem causing all of this. I will look into the information you raised regarding invoking the GC. – jmehrsam Sep 16 '19 at 12:56
  • 1
    Your file is being used by another process...I mean that's your problem. You need to figure out which process. I'm not sure how anyone here can help, you should pay attention to what Hans has already told you – Liam Sep 16 '19 at 13:02
  • While I do realize that this is my problem, I can actually find any task in the task manager that is using the file. There is no process from Excel running, neither from any other software that could be used to edit/alter the file. I was much rather referring to a tool (I am using Windows) or command that makes it easier for me to find that process. On my own, I found the get-process cmdlet that made me find the process. I will try terminating that process now and see if that makes any difference. – jmehrsam Sep 16 '19 at 13:09

1 Answers1

2

As Hans Passant and Liam explained in their comments, my file was used by another process. Whilst the process was not listed in the taskmanager, it was still running. I searched the web to get another way to determine the process and how to terminate it and I found two cmdlets: First, I started PowerShell with administrator privileges and used the cmdlet "Get-Process [-name]" to check if there is a process with the name "EA_Excel_ReadRange.

Get-Process EA_Excel_ReadRange

Second, I used the ID returned by this cmdlet in the cmdlet "Stop-Process [-ID]" to kill the process that caused the file not being able to be accessed.

Stop-Process -id 21944
jmehrsam
  • 21
  • 6