title: System.AccessViolationException: Attempted to read or write protected memory
body: <p>I am trying to open and Excel 2000 file. When I try this with Excel 2007 it is working fine but when I checked it on the development server it is not working. It is thowing an error while opening the excel file. I am using the below code. </p>
  
  <pre><code> // file
   string sFile = @"c:\testfile.xls";
  
   // create excel file
   Excel.Application oExcel;
   Excel.Workbook oBook;
   Excel.Worksheet oSheet;
   DataTable dt = new DataTable();
   DataRow dr = dt.NewRow();
  
  
  oExcel = new Excel.ApplicationClass();
  oBook = oExcel.Workbooks.Add(Type.Missing);
  oBook = oExcel.Workbooks.Open(sFile, 0, false, 5, "", "", true, 
      Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 
      0, true, true, false);
  oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(2);
  oSheet.Cells[2, 1] = "Successfully Created";
  oSheet.Cells[2, 2] = "Sample excel updated";
  oExcel.DisplayAlerts = false;
  
  // save &amp; close
  oBook.Close(true, sFile,false);
  oExcel.Quit();
  Marshal.ReleaseComObject(oExcel);
  oExcel = null;
  GC.Collect();
  </code></pre>
  
  <p>The error msg is  the following:</p>
  
  <blockquote>
    <p>"Unhandled Exception: System.AccessViolationException: Attempted to
    read or write  protected memory. This is often an indication that
    other memory is corrupt.    at
    Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object
    Upda teLinks, Object ReadOnly, Object Format, Object Password, Object
    WriteResPasswor d, Object IgnoreReadOnlyRecommended, Object Origin,
    Object Delimiter, Object Edi table, Object Notify, Object Converter,
    Object AddToMru, Object Local, Object Co rruptLoad)   at
    TestExcel.Program.Main(String[] args)"</p>
  </blockquote>