deleting an excel file in C#, Taskmanager process is not stopped

Tag: excel Author: xinye1970 Date: 2009-09-08

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)
            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 ?



Best Answer

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

Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;

/* do your stuff */



Other Answer1

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.

Other Answer2

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