tags:

views:

355

answers:

3

Hi,

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

+1  A: 

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.

Jay Riggs
A: 

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

Eric J.
+1  A: 

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