|I wrote an application that writes to disk a certain excel document from sql database, then opens it edit.
After user finished editing the document in Excel, the application asks in a dialog if user wants to update the changes to DB. If yes, updates the file in the database.
The problem is, that after Excel window is closed and file updated to db, the Excel process is still running. It can be seen in Process Manager,
and causes incorrect behaviour of new Excel windows. After opening 50 documents, there are 50 processes. That causes serious errors in Excel, up to "OLE library error", which can be cured so far by re-installing Office.
The question is: How to kill Excel properly?
Here is the code:
private void OpenExcel(string filename)
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
myproc.EnableRaisingEvents = true;
//a special exitbox is assigned to each document
ExitBox windowAfterExit = new ExitBox(filename,this,myproc);
myproc.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
myproc.StartInfo.FileName = filename;
myproc.StartInfo.Verb = "open";
// at a moment we can work with one document only
public class ExitBox : System.Windows.Forms.Form
public void Excel_Exited(object sender,EventArgs e)
this.Visible = true;
private void Exit()