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,207
  • 7
  • 36
  • 50

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
  • 53,046
  • 9
  • 139
  • 151
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,829
  • 22
  • 107
  • 159
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.
  • 147,927
  • 63
  • 340
  • 553