Try this:
System.IO.FileStream fs;
try
{
startTime = DateTime.Now;
fs = new System.IO.FileStream(FullfilePath, System.IO.FileMode.Open, FileAccess.ReadWrite);
IExcelDataReader excelReader2007 = ExcelReaderFactory.CreateOpenXmlReader(fs);
excelReader2007.IsFirstRowAsColumnNames = false;
DataSet result = excelReader2007.AsDataSet();
if (result.Tables.Count > 0)
{
ds = result;
}
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Success), "Table generated from excel");
}
catch (Exception ex)
{
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Failure), Convert.ToString(ex.Message));
}
finally
{
if (fs != null){
try{
fs.Close();
fs.Dispose();
}
catch(Exception ex){
//Error handling for being unable to close file
}
}
}
It sounds like you are leaving the file open by a previous execution run and haven't closed it properly, this should hopefully solve your issue. If not, check to see if something else is still using the file
Or you could use the using
function so that it closes off the file use for you, see here
[Edit] Example of using
function (You may also need to use using
on your IExcelDataReader, but I haven't checked the API):
try
{
using(System.IO.FileStream fs = new System.IO.FileStream(FullfilePath, System.IO.FileMode.Open, FileAccess.ReadWrite))
{
startTime = DateTime.Now;
IExcelDataReader excelReader2007 = ExcelReaderFactory.CreateOpenXmlReader(fs);
excelReader2007.IsFirstRowAsColumnNames = false;
DataSet result = excelReader2007.AsDataSet();
if (result.Tables.Count > 0)
{
ds = result;
}
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Success), "Table generated from excel");
}
}
catch (Exception ex)
{
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Failure), Convert.ToString(ex.Message));
}
[Edit2]
Issue is being caused by NTFS systems setting a flag saying it's unsafe, you need to use the following code and call it before accessing the file to unblock it:
public class FileUnblocker {
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteFile(string name );
public bool Unblock(string fileName) {
return DeleteFile(fileName+ ":Zone.Identifier");
}
}
Source
So your resulting code would be:
try
{
if (new FileUnblocker().Unblock(FullfilePath))
{
using(System.IO.FileStream fs = new System.IO.FileStream(FullfilePath, System.IO.FileMode.Open, FileAccess.ReadWrite))
{
startTime = DateTime.Now;
IExcelDataReader excelReader2007 = ExcelReaderFactory.CreateOpenXmlReader(fs);
excelReader2007.IsFirstRowAsColumnNames = false;
DataSet result = excelReader2007.AsDataSet();
if (result.Tables.Count > 0)
{
ds = result;
}
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Success), "Table generated from excel");
}
}
else{
//Issue with unblocking, deal with it here
}
}
catch (Exception ex)
{
InsertExecLogDetails(startTime, DateTime.Now, Convert.ToString(Common.EventNames.GenerateDataTableFromExcel), Convert.ToString(Common.StatusEnum.Failure), Convert.ToString(ex.Message));
}