2

I am opening an Excel file via

app = new Microsoft.Office.Interop.Excel.Application();
wb = app.Workbooks.Open(fromFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

Then I am manipulating the cells, which works fine. I save the file to antother Folder, than I opened it in.

wb.SaveAs(toFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, Type.Missing, Type.Missing, true);

I Close the app and the workbook:

wb.Close(true, Type.Missing, Type.Missing);
app.Quit();

When I now want to move the original file by File.Move(), I get the error:

The process cannot access the file because it is being used by another

process.

Am I missing something?

edit:

Thank you very much guys. It has been the missing release of the objects.

Marshal.FinalReleaseComObject(object)

edit 2:

Well, but now I could run it once and now I always get the error:

This file is currently not available for use on this computer.

Combined with a COMException

The object invoked has disconnected from its clients.

Kai A
  • 23
  • 3
  • This might help: https://social.msdn.microsoft.com/Forums/vstudio/en-US/5e680538-ed9b-4894-b358-bb72a9b7f531/the-process-cannot-access-the-file-it-is-being-used-by-another-process-to-close-excel-file-before?forum=csharpgeneral – Derek Sep 19 '17 at 15:12
  • Are you calling `File.Move` just directly after `app.Quit`? – dymanoid Sep 19 '17 at 15:12
  • Press [Ctrl]+[Alt]+[Del] and make sure there's no Excel process locking the file. – derloopkat Sep 19 '17 at 15:14
  • I even wrote the move part into another Action and created a new button, I first have to click. When I don't manipulate the file before, moving works. – Kai A Sep 19 '17 at 15:14
  • there is still an Excel process open in the task manager, which shouldnt be. How can I Close it? – Kai A Sep 19 '17 at 15:16
  • Did you try this? https://sayanghosh.wordpress.com/2009/02/04/a-com-heartbreak-microsoft-office-interop-excel-workbook-and-the-process-cannot-access-the-file-because-it-is-being-used-by-another-process/ – Rick S Sep 19 '17 at 15:21

2 Answers2

4

You need to use

Marshal.ReleaseComObject(Object_to_be_released)

for all objects used in the interop service.

_xlApp.Quit();
Marshal.ReleaseComObject(_xlApp);
R. Roe
  • 609
  • 7
  • 18
  • hmm. Is it a Standard Namespace? I can't find it, to use it. – Kai A Sep 19 '17 at 15:23
  • its in `using System.Runtime.InteropServices` – R. Roe Sep 19 '17 at 15:31
  • As i edited. Now I can run it perfectly once, but now it seems that the file cannot be read again. – Kai A Sep 20 '17 at 09:05
  • According to the edits of your post, those are different questions from the one you originally asked. If you google those errors you will find lots of articles that deal with those same errors. – R. Roe Sep 20 '17 at 14:31
0

Before you move excel file from source folder to destination folder you need to release all the references related to excel file. References which you want to release is listed in answer given by Grand Crofton Please refer his answer: How to release excel process?

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • As i edited. Now I can run it perfectly once, but now it seems that the file cannot be read again. – Kai A Sep 20 '17 at 09:05