I have a class that reads excel worksheet names using LinqToExcel.
public class ExcelREader: IReader
{
public IEnumerable<string> GetWorksheetNames(_fileName)
{
using (var excelQueryFactory = new ExcelQueryFactory(_fileName))
{
return excelQueryFactory.GetWorksheetNames();
}
}
}
And I am using in my asp.net mvc project.
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
var path = Path.Combine(Server.MapPath("~/App_Data"), file.FileName);
file.SaveAs(path);
var reader = new ExcelREader();
var items = reader.GetWorksheetNames(path);
File.Delete(path);
return View();
}
But File.Delete(path) throws an exception. "The process cannot access the file 'XXX' because it is being used by another process."
I used using keyword. And used GC.Collect(); or GC.WaitForPendingFinalizers(); methods but not worked.
Set the UsePersistentConnection property to false: