Reputation: 4379
I have a .NetCore (.Net 8.0) application that uses DevExpress XPO.
The application creates and keys a backup file. I create a series of rolling backups that keeps a number of backups on disk and then deletes the oldest when the threshold is exceeded.
The application is getting "process cannot access the file" exceptions when I attempt to delete a backup file created during the current session.
Within the application, I've created a simplified snippet of code (with marking the database) which recreates the exception every time it's run.
Consider the following code snippet:
var connectionString = "XpoProvider=SQLite;Data Source=filename.db";
_ = XpoDefault.GetConnectionProvider(connectionString, AutoCreateOption.DatabaseAndSchema);
var backupDBFileName = @"BackupDBFile.db";
if (!File.Exists(backupDBFileName))
{
File.Copy("filename.db", backupDBFileName);
}
var connectionString = SQLiteConnectionProvider.GetConnectionString(backupDBFileName);
var provider = XpoDefault.GetConnectionProvider(connectionString, AutoCreateOption.DatabaseAndSchema, out IDisposable[] objsToDispose);
var dictionary = new DevExpress.Xpo.Metadata.ReflectionDictionary();
var dataLayer = new ThreadSafeDataLayer(dictionary, provider);
XpoDefault.DataLayer = dataLayer;
// Dispose/Disconnect
// If there are objects that needs disposing along with the Data Layer, dispose of them
if (objsToDispose != null)
{
foreach (var obj in objsToDispose)
{
obj.Dispose();
}
}
XpoDefault.DataLayer.Dispose();
XpoDefault.DataLayer = null;
// Trying to cleanup
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
try
{
if (File.Exists(backupDBFileName))
{
File.Delete(backupDBFileName);
}
}
catch (Exception ex) {}
When I attempt to delete the backup file [File.Delete()], I get the following exception..
The process cannot access the file 'BackupDBFile.db' because it is being used by another process.
Upvotes: 0
Views: 55