How to Delete the File Generated from directory set in Options.CachedFileFolder if there is exception occured during save

Hi.

I am using Lightcells API to save a xls workbook. When there is exception occurred during saving, the generated file in Options.CachedFileFolder is not deleted.

Just want to ask on how to delete the file or the directory set on Options.CachedFileFolder when there is any exception occurred. Currently when we try to delete it using System.IO it says that the file is being used by another process eventhough that the Workbook is disposed.

Exception:
System.IO.IOException: ‘The process cannot access the file ‘Aspose.Cellsad4b8ea3-dd48-4405-9985-866b3633ae8b_Cells0.dat’ because it is being used by another process.’

We just want to have a clean up whether the save is successful or not. When saving successfully there is no problem.

Sample Code:

class Program {
		static void Main(string[] args) {
			int numberOfRows = 1000;
			DataTable testDataTable = new DataTable();
			string testFile = @"C:\Test\file.xls";
			DataRow dataRow;

			// Just using dummy data here for testing..
			// Initialize the columns.
			testDataTable.Columns.Add("TEST_COLUMN1");
			testDataTable.Columns.Add("TEST_COLUMN2");

			// Insert data.
			for (int currentRowIndex = 0; currentRowIndex < numberOfRows; currentRowIndex++) {
				dataRow = testDataTable.NewRow();
				dataRow[0] = string.Format("TEST_COLUMN1_VALUE_{0}", currentRowIndex);
				dataRow[1] = string.Format("TEST_COLUMN2_VALUE_{0}", currentRowIndex);
				testDataTable.Rows.Add(dataRow);
			}

			XlsSaveOptions options;
			Workbook workBook;

			workBook = new Workbook();
			options = new XlsSaveOptions(SaveFormat.Excel97To2003);

			options.CachedFileFolder = @"C:\Temp";

			options.LightCellsDataProvider = new XlsLightCellsDataProvider(testDataTable, testDataTable.Columns.Count, testDataTable.Rows.Count);

			try {
				// Save the workbook.
				if(!Directory.Exists(options.CachedFileFolder))
					Directory.CreateDirectory(options.CachedFileFolder);

				workBook.Save(testFile, options);
			}
			catch (Exception ex) {
				Console.WriteLine("An error occured when saving the file. " + ex.Message);
			}
			finally {
				workBook.Dispose();
				// Delete the temp directory from Cached File Folder since we cannot check the file generated by aspose.
				if (Directory.Exists(options.CachedFileFolder)) {
					// Error occurs here when deleting the folder including the generated file.If the save is unsuccesfull
					Directory.Delete(options.CachedFileFolder, true);
				}
			}
		}
	}

	class XlsLightCellsDataProvider : LightCellsDataProvider {

		private int _currentColumnIndex = -1;
		private int _currentRowIndex = -1;
		private DataTable _dataTable;
		private int _maxColumns;
		private int _maxRows;

		public XlsLightCellsDataProvider(DataTable dataTable, int maxColumns, int maxRow) {
			_maxRows = maxRow;
			_dataTable = dataTable;
			_maxColumns = maxColumns;
		}

		#region LightCellsDataProvider Members

		public bool IsGatherString() {
			return false;
		}
		public int NextCell() {
			_currentColumnIndex++;
			if (_currentColumnIndex < _maxColumns) {
				return _currentColumnIndex;
			}
			else {
				return -1;
			}
		}
		public int NextRow() {
			_currentRowIndex++;
			if (_currentRowIndex <= _maxRows) {
				_currentColumnIndex = -1;
				return _currentRowIndex;
			}
			else {
				return -1;
			}
		}
		public void StartCell(Cell cell) {
			Style cellStyle;
			object cellValue;

			if (_currentRowIndex == 0) {
				// Write the column headers
				cell.PutValue(_dataTable.Columns[_currentColumnIndex].ColumnName);
			}
			else {
				cellValue = _dataTable.Rows[_currentRowIndex - 1][_currentColumnIndex];
				if (cellValue is DateTime) {
					cell.PutValue((DateTime)cellValue);

					// Format the current cell with date format
					cellStyle = cell.GetStyle();
					cellStyle.Number = 15;
					cell.SetStyle(cellStyle);
				}
				else {
					cell.PutValue(cellValue);
				}

				// Test to produce exception..
				throw new Exception("TestException");
			}
		}
		public void StartRow(Row row) {
		}
		public bool StartSheet(int sheetIndex) {
			return true;
		}

		#endregion
	}

Looking forward for your response.
Thanks

@jsaez
By creating sample file and using sample code for testing on the latest version v24.4, we were able to reproduce the problem. An exception occurred when deleting the cache folder after processing the file.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-55573

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

@johnsaez,

We are pleased to inform you that your issue (“CELLSNET-55573”) has been resolved. The fix will be included in our upcoming release (Aspose.Cells v24.5) that we plan to release in the second week of May 2024. You will be notified when the next version is released.

The issues you have found earlier (filed as CELLSNET-55573) have been fixed in this update. This message was posted using Bugs notification tool by johnson.shi