title: C# = Why are Excel processes not ending?
body: <p>I have the following code:</p>
  
  <pre><code>private bool IsMousetrapFile(string path)
      {
  
          logger.Log(validateFileMessage + path);
  
          Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
          Excel.Workbooks workbooks = xlApp.Workbooks;
          Excel.Workbook xlWorkBook = workbooks.Open(path, Type.Missing, Type.Missing,
              Type.Missing, Type.Missing, Type.Missing,
              Type.Missing, Type.Missing, Type.Missing, Type.Missing,
              Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
          Excel.Sheets worksheets = (Excel.Sheets)xlWorkBook.Worksheets;
          Excel.Worksheet ws = null;
  
  
          foreach (string sheet in expectedWorksheets)
          {
  
              try
              {
  
                  ws = (Excel.Worksheet)worksheets.get_Item(sheet);
                  logger.Log(validMousetrapFileMessage + path);
              }
              catch
              {
                  logger.Log(validateSheetError + sheet + ": " + path);
                  if (ws != null)
                      Marshal.ReleaseComObject(ws);
                  Marshal.ReleaseComObject(worksheets);
                  Marshal.ReleaseComObject(xlWorkBook);
                  Marshal.ReleaseComObject(workbooks);
                  Marshal.ReleaseComObject(xlApp);
  
                  return false;
  
              }
  
          }
  
          if (ws != null)
              Marshal.ReleaseComObject(ws);
          Marshal.ReleaseComObject(worksheets);
          Marshal.ReleaseComObject(xlWorkBook);
          Marshal.ReleaseComObject(workbooks);
          Marshal.ReleaseComObject(xlApp);
  
          return true;
  
  
      }
  </code></pre>
  
  <p>Effectively, it checks if an Excel workbook contains specific sheets.  Irrespective of whether there is or isn't, I want to Excel processes to end.  However, each time a new workbook is open, a new process is added, and never deleted?</p>
  
  <p>PS.  I know there is duplicate code there....it shall be tidied soon :)</p>