1

On the move statement below, I get:

System.IO.IOException: The process cannot access the file because it is being used by another process.

But it only occurs if I open a connection and run the SQL statement. If I comment those out and run only the move, there is no problem. Does anyone have any idea why that even though the connection to the database is closed, it appears to still be in use?

if (File.Exists(dataDir + @"\database.s3db"))
{
    var connectionString = "Data Source=" + dataDir + @"\database.s3db;Page Size=4096;Password=" + pw;
    using (connection = new SQLiteConnection(connectionString))
    {
        connection.Open();
        var integrityCheck = connection.CreateCommand();
        integrityCheck.CommandText = "PRAGMA INTEGRITY_CHECK";
        string result = integrityCheck.ExecuteScalar().ToString();
        connection.Close();
        connection.Dispose();
        if (result != "ok")
        {
            File.Move(dataDir + @"\database.s3db", dataDir + @"\database2.s3db");
        }
    }
}
baddrivers
  • 25
  • 4
  • 5
    This question may be useful https://stackoverflow.com/questions/8511901/system-data-sqlite-close-not-releasing-database-file – Serg Jan 04 '21 at 18:00
  • Collect and WaitForPendingFinalizers seems to work. Thanks. I'm not sure how I missed that post. – baddrivers Jan 04 '21 at 18:18

0 Answers0