I am trying to SaveAs() a sheet to csv file and immediately after load that csv file into string, using C# VSTO. The issue is that unless I Close() the workbook I can't access newly created csv file. The error is related to "file locked" issue. If I close the workbook or copy it to blank workbook then I always able to access that file, however these are not ideal solutions. Any ideas, on how to avoid file lock?
Excel.Worksheet sheet = workbook.Sheets[sheetName];
sheet.Activate();
sheet.Unprotect();
sheet.SaveAs(fileName, Excel.XlFileFormat.xlCSV, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
workbook.Close(); <-- Don't want that happen
// Get the Input CSV.
string contents = File.ReadAllText(InputCsvFileName);