I am not able to read the CSV file immediately after converting an Excel sheet to CSV. I am getting error: "The process cannot access the file .. because it is being used by another process."
My code is:
csvFilePath = AppSettings.exportFileFolderPath + @"\Temp002.csv";
ws.SaveAs(csvFilePath, Excel.XlFileFormat.xlCSV, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
allLines = File.ReadAllLines(csvFilePath);
At this point, if I put a breakpoint and try to rename the file in Windows Explorer, I get the same error in Explorer too. But immediately after the debugger throws an exception and the program ends, I am able to rename the file in Explorer.
What is the issue and the solution?
EDIT 1: This seems to be working:
string csvFilePath1 = AppSettings.exportFileFolderPath + @"\Temp001.001";
ws.SaveAs(csvFilePath1, Excel.XlFileFormat.xlCSV, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
ws = wSheets.get_Item("Dummy") as Excel.Worksheet;
string csvFilePath2 = AppSettings.exportFileFolderPath + @"\Temp001.002";
ws.SaveAs(csvFilePath2, Excel.XlFileFormat.xlCSV, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
string[] allLines = File.ReadAllLines(csvFilePath1);
But just wondering if there is a still better way?