title: Can't figure out how to close excel process when finished with my program
body: <p>I've looked at all the possible answers on stackoverflow and still cant figure out how to solve my problem.</p>
  <p>My program creates an excel file, exports data into the file and then should close it. It doesn't close it completely however and the excel process is still running in the background which makes using my code more than once impossible as it always throws up an error.</p>
  <p>I start of by creating my excel object</p>
  <pre><code>Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
  </code></pre>
  <p>I then assign variables to everything I need to use:</p>
  <pre><code>Excel.Workbook xlWorkBook;
  Excel.Worksheet xlWorkSheet;
  object misValue = System.Reflection.Missing.Value;
  xlWorkBook = xlApp.Workbooks.Add(misValue);
  xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
  </code></pre>
  <p>then do all my manipulation.</p>
  <p>To close the excel process I use:</p>
  <pre><code>xlWorkBook.SaveAs(@&quot;C:\Users\dphillips\Desktop\Projects\C#\Exported Excel File&quot;, Excel.XlFileFormat.xlWorkbookNormal,
  misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
  
  xlWorkBook.Close(true, &quot;Exported Excel File&quot;, misValue);
  xlApp.Quit();
  
  Marshal.FinalReleaseComObject(xlWorkSheet);
  Marshal.FinalReleaseComObject(xlWorkBook);
  Marshal.FinalReleaseComObject(xlApp);
  xlWorkSheet = null;
  xlApp = null;
  xlWorkBook = null;
  </code></pre>
  <p>The process doesn't close though. I used <a href="http://csharp.net-informations.com/excel/csharp-create-excel.htm" rel="nofollow noreferrer">http://csharp.net-informations.com/excel/csharp-create-excel.htm</a> as my template to do this as I'm new to C# and don't have enough experience to do it on my own.</p>
  <p>Any advice on how to correctly close the program would be greatly appreciated</p>
  <p>EDIT:
  Since I can't seem to get any luck with it I will just post my whole program. Maybe then you could tell me exactly where I'm going wrong</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.IO;
  using System.Text.RegularExpressions;
  using System.Runtime.InteropServices;
  using Excel = Microsoft.Office.Interop.Excel;
  
  namespace FormPractice
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
      private void Form1_Load(object sender, EventArgs e)
      {
  
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          FolderBrowserDialog FBD = new FolderBrowserDialog();
  
          if (FBD.ShowDialog() == DialogResult.OK)
          {
              textBox1.Text = FBD.SelectedPath;
              
              //Creating excel object
              Excel.Application xlApp = new Excel.Application();
              xlApp.Visible = true;
  
              //Check to see that excel is installed.
              if (xlApp == null)
              {
                  MessageBox.Show(&quot;Excel is not properly installed!!&quot;);
                  return;
              }
              
              // regular expressions to check for the ID number in the file and the issue number of the document
              Regex regularExpressionDocID = new Regex(@&quot;\d{3}[A-Z]\W\d{4}\W\d{6}\W\d{3}\W[A-Z]\d{2}&quot;);
              Regex regularExpressionIssueNumber = new Regex(@&quot;[i,I]s+(ue)*.?\d{1,2}$&quot;);
  
              var filesToBeRead = Directory.GetFiles(FBD.SelectedPath, &quot;*.*&quot;, SearchOption.AllDirectories)
                                               .Where(fileNameBeingProcessed =&gt;
                                               (fileNameBeingProcessed.EndsWith(&quot;.txt&quot;)
                                               || fileNameBeingProcessed.EndsWith(&quot;.docx&quot;)
                                               || fileNameBeingProcessed.EndsWith(&quot;.doc&quot;)
                                               || fileNameBeingProcessed.EndsWith(&quot;.xls&quot;)
                                               || fileNameBeingProcessed.EndsWith(&quot;.pdf&quot;)
                                               || fileNameBeingProcessed.EndsWith(&quot;.docm&quot;)
                                               || fileNameBeingProcessed.EndsWith(&quot;.xlsm&quot;)
                                               || fileNameBeingProcessed.EndsWith(&quot;.xlsx&quot;)))
                                               .ToList();
  
              var xlWorkBooks = xlApp.Workbooks;
              object misValue = System.Reflection.Missing.Value;
              var xlWorkBook = xlWorkBooks.Add(misValue);
              var xlWorkSheets = xlWorkBook.Worksheets;
              var xlWorkSheet = (Excel.Worksheet)xlWorkSheets.get_Item(1);
  
  
              string comparingString;
              int counter = 2;
  
              // setting all headers
              xlWorkSheet.Cells[1, 1] = &quot;Document Number&quot;;
              xlWorkSheet.Cells[1, 2] = &quot;Name&quot;;
              xlWorkSheet.Cells[1, 3] = &quot;System Revision&quot;;
              xlWorkSheet.Cells[1, 4] = &quot;Revision&quot;;
              xlWorkSheet.Cells[1, 5] = &quot;Type&quot;;
              xlWorkSheet.Cells[1, 6] = &quot;Old Name&quot;;
              xlWorkSheet.Cells[1, 7] = &quot;File Name&quot;;
              xlWorkSheet.Cells[1, 8] = &quot;Directory&quot;;
              xlWorkSheet.Cells[1, 9] = &quot;Owner&quot;;
  
              foreach (string file in filesToBeRead)
              {
                  string documentID, documentName, revision, oldName, directoryOfFile, stringToBeManipulated;
                  
                  comparingString = Path.GetFileNameWithoutExtension(file);
  
                  Match matchesFormat = regularExpressionDocID.Match(comparingString);
                  Match matchesIssue = regularExpressionIssueNumber.Match(comparingString);
  
                  // All manipulations to get each of the required fields for the ARAS upload xlsx
                  if (matchesFormat.Success)
                  {
                      stringToBeManipulated = (Path.GetFileNameWithoutExtension(file)); // document ID sectiom
                      documentID = stringToBeManipulated.Substring(0, 24);
                      xlWorkSheet.Cells[counter, 1] = documentID;
  
                     
                      if (matchesIssue.Success)
                      {
                          //If &quot;issue&quot; is found then gives name(from char [25] and inserts issue into the correct cells
                          revision = matchesIssue.Value;
                          documentName = stringToBeManipulated[25..matchesIssue.Index];
                      }
                      else
                      {
                          //If &quot;issue&quot; is not found fills the issue column with a &quot;-&quot; and makes the entire string the name.
                          revision = &quot;-&quot;;
                          documentName = stringToBeManipulated[25..stringToBeManipulated.Length];
                      }
                      xlWorkSheet.Cells[counter, 2] = documentName;
                      xlWorkSheet.Cells[counter, 3] = revision;
                      xlWorkSheet.Cells[counter, 4] = revision;
  
                      oldName = (Path.GetFileName(file));
                      xlWorkSheet.Cells[counter, 6] = oldName;
                      xlWorkSheet.Cells[counter, 7] = oldName;
  
                      directoryOfFile = Path.GetDirectoryName(file);
                      xlWorkSheet.Cells[counter, 8] = directoryOfFile;
                      counter++;
                  }
  
              }
  
              xlWorkBook.SaveAs(@&quot;C:\Users\dphillips\Desktop\Projects\C#\Exported_Old_Doc_ID&quot;, Excel.XlFileFormat.xlWorkbookNormal,
              misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
  
              xlWorkBook.Close(true, &quot;Exported Excel File&quot;, misValue);
              xlApp.DisplayAlerts = false;
              xlApp.Quit();
  
  
              //Releasing all objects to stop memory leaks
  
              GC.WaitForPendingFinalizers();
              GC.Collect();
              GC.WaitForPendingFinalizers();
              GC.Collect(); 
              GC.WaitForPendingFinalizers();
              GC.Collect();
              GC.WaitForPendingFinalizers();
              GC.Collect();
              GC.WaitForPendingFinalizers();
              GC.Collect();
              Marshal.FinalReleaseComObject(xlWorkSheet);
              Marshal.FinalReleaseComObject(xlWorkBook);
              Marshal.FinalReleaseComObject(xlWorkSheets);
              Marshal.FinalReleaseComObject(xlWorkBooks);
              Marshal.FinalReleaseComObject(xlApp);
  
              xlWorkSheet = null;
              xlApp = null;
              xlWorkBook = null;
              xlWorkBooks = null;
              xlWorkSheets = null;
  
  
  
              MessageBox.Show(@&quot;Excel file created , you can find the file C:\Users\dphillips\Desktop\Projects\C#\Directory to pull&quot;);
  
          }
          GC.Collect();
          Close();
  </code></pre>