title: Save and delete Excel file saved using HttpPostedFileBase
body: <p>I am uploading an Excel file and extracting data from that and saving it into a database. I am using MVC4 .NET Framework. This is my code from class:</p>
  
  <pre><code> public static void Upload(HttpPostedFileBase File)
          {
              NIKEntities1 obj = new NIKEntities1();
              MyApp = new Excel.Application();
              MyApp.Visible = false;
              string extension = System.IO.Path.GetExtension(File.FileName);
  
              string pic = "Excel" + extension;
  
              string path = System.IO.Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Excel"), pic);
  
              File.SaveAs(path);
  
  
              MyBook = MyApp.Workbooks.Open(path);
              MySheet = (Excel.Worksheet)MyBook.Sheets[1]; // Explicit cast is not required here
              int lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
              List&lt;Employee&gt; EmpList = new List&lt;Employee&gt;();
  
              for (int index = 2; index &lt;= lastRow; index++)
              {
                  System.Array MyValues = (System.Array)MySheet.get_Range("A" +
                     index.ToString(), "B" + index.ToString()).Cells.Value;
                  EmpList.Add(new Employee
                  {
                      BatchID = MyValues.GetValue(1, 1).ToString(),
                      BatchName = MyValues.GetValue(1, 2).ToString()
  
                  });
              }
  
              for (int i = 0; i &lt; EmpList.Count; i++)
              {
                  int x=obj.USP_InsertBatches(EmpList[i].BatchID, EmpList[i].BatchName);
  
              }    
          }
      }
      class Employee
      {
          public string BatchID;
          public string BatchName;
      }
  </code></pre>
  
  <p>This code is working perfectly the first time but next time it says that file is currently in use. So I thought of deleting the file at the end of code using the following line:</p>
  
  <pre><code>File.Delete(path);
  </code></pre>
  
  <p>But this line threw error:</p>
  
  <blockquote>
    <p>HttpPostedFileBase does not contain definition for Delete</p>
  </blockquote>
  
  <p>Also, if I don't write this line and try to execute code again it says that it can't save because a file exists with same name and could not be replaced because it is currently in use.</p>
  
  <p>What should I do to get rid of this:</p>
  
  <pre><code>  (File.Delete()) Error
  </code></pre>
  
  <p>Any other way of accessing the Excel file which I am receiving without saving will also be very helpful because I have to just access the data one time.</p>