title: C#- Microsoft.Office.Interop.Excel copy a specific row from an excel to another excel instance and delete that row afterwards
body: <p>What I'm trying to achieve is that, based on a email verification, to copy the entire row from one excel instance to another one and delete that row afterwards. The thing is that none of my methods ( copy the row and delete it ) work at this point and for some reason, once I run my code, both files become <code>read only</code>.</p>
  <p>Sorry for any mistake made, but this is my first question here.</p>
  <p>Do you see anything wrong regarding my code? Or can you please explain how this should look like?</p>
  <p>Here is what I've tried so far:</p>
  <pre><code>using Microsoft.Office.Interop.Excel;
  using System;
  using System.Runtime.InteropServices;
  using System.Text.RegularExpressions;
  
  namespace CleanUpUserList
  {
      public class Program
      {
  
          static Regex ValidEmailRegex = CreateValidEmailRegex();
          private static string invalidUserFileName = @&quot;C:\Users\Sebastian Cocirla\Desktop\To.xlsx&quot;;
          private static string validUserFileName = @&quot;C:\Users\Sebastian Cocirla\Desktop\From.xlsx&quot;;
          private static string saveAsFileName = &quot;SSO Member List NON SECUTIX - INValid.xlsx&quot;;
          private static string mailToMatchForInvalidation = &quot;mailinator&quot;;
          private static char mailSeparator = '@';
          private static char domainSeparator = '.';
  
          static void Main()
          {
              CleanUpExcelFile();
          }
  
          public static void CleanUpExcelFile()
          {
              int newWorkbookIndex = 0;
  
              Application app = new Application();
  
              Workbook validUserFile = app.Workbooks.Open(validUserFileName);
              Worksheet validUserWorksheet = validUserFile.Sheets[1];
  
              Workbook invalidUserFile = app.Workbooks.Open(invalidUserFileName);
              Worksheet invalidUserWorksheet = invalidUserFile.Sheets[1];
  
              Range range = validUserWorksheet.UsedRange;
  
              int rows = range.Rows.Count;
  
              validUserWorksheet.Activate();
              invalidUserWorksheet.Activate();
  
              for (int rowIndex = 2; rowIndex &lt; rows; rowIndex++)
              {
  
                  var emailAddress = range.Cells[rowIndex, 8].Value2.ToString();
  
                  if (!EmailIsValid(emailAddress))
                  {
                      newWorkbookIndex++;
  
                      CopyRow(validUserFile, invalidUserFile, validUserWorksheet, invalidUserWorksheet, rowIndex, newWorkbookIndex);
  
                      ((Range)validUserWorksheet.Rows[rowIndex]).Delete(XlDeleteShiftDirection.xlShiftUp);
                  }
              }
  
              GC.Collect();
              GC.WaitForPendingFinalizers();
  
              Marshal.ReleaseComObject(range);
              Marshal.ReleaseComObject(validUserWorksheet);
              Marshal.ReleaseComObject(invalidUserWorksheet);
  
              validUserFile.Close();
              invalidUserFile.Close();
              Marshal.ReleaseComObject(validUserFile);
              Marshal.ReleaseComObject(invalidUserFile);
  
              app.Quit();
              Marshal.ReleaseComObject(app);
          }
  
          private static void CopyRow(Workbook validUserFile, Workbook invalidUserFile, Worksheet validUserWorksheet,
              Worksheet invalidUserWorksheet, int rowIndex, int newWorkbookIndex)
          {
  
              var selectedRow = validUserWorksheet.Range[&quot;A1:M1&quot;];
              var destinationRow = invalidUserWorksheet.Range[&quot;A2:M2&quot;];
  
              selectedRow.Copy(destinationRow);
              validUserFile.Save();
              invalidUserFile.Save();
          }
  
          private static Regex CreateValidEmailRegex()
          {
              string validEmailPattern = @&quot;^(?!\.)(&quot;&quot;([^&quot;&quot;\r\\]|\\[&quot;&quot;\r\\])*&quot;&quot;|&quot;
                                         + @&quot;([-a-z0-9!#$%&amp;'*+/=?^_`{|}~]|(?&lt;!\.)\.)*)(?&lt;!\.)&quot;
                                         + @&quot;@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$&quot;;
  
              return new Regex(validEmailPattern, RegexOptions.IgnoreCase);
          }
  
          private static bool EmailIsValid(string emailAddress)
          {
              var splitEmailHost = emailAddress.Split(mailSeparator);
              var hostLength = splitEmailHost[1].Split(domainSeparator)[0].Length;
  
              bool isValid = ValidEmailRegex.IsMatch(emailAddress)
                             &amp;&amp; !splitEmailHost[1].Contains(mailToMatchForInvalidation)
                             &amp;&amp; hostLength &gt; 1;
  
              return isValid;
          }
  
      }
  }
  </code></pre>
  <p>Any help will be greatly appreciated!</p>