2

I am working on a wpf application.

I am copying the data to excel sheet from database and saving the file and closing it once the operation is completed.

My question is:

How to stop the Process(EXCEL.EXE) in TaskManager->Processes ?

I have to delete the file after the operation is completed. I have written a pieceof code to stop the process in taskmanager, but didnt work..

  private void EndExcelAPP(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        }
        catch
        {
        }
        finally
        {
            obj = null;
        }
    }

I cant delete it since using this too.. since it says the process is used by another process.

Please help me how to stop this process programatically in c# and delete the fiile ?

Thanks

Ramm

user301016
  • 2,137
  • 7
  • 35
  • 49

3 Answers3

2

Sounds like you're not cleaning up after yourself (or more accurately, after Excel). Check out:

How to properly clean up Excel interop objects in C#

After Excel has stopped running in the background you should be able to delete the file. And you shouldn't have to write code to kill the process.

Community
  • 1
  • 1
Jay Riggs
  • 52,110
  • 9
  • 138
  • 148
2

You have to make sure you close the workbook and exit the application:

Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;

/* do your stuff */

xlWorkbook.Close();
xlApp.Exit();

Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
scottm
  • 27,151
  • 22
  • 104
  • 158
0

Did you close the workbook before releasing it? That's what I do and it works for me. Here's an example.

Eric J.
  • 143,945
  • 62
  • 324
  • 540