can not remove file afterwards

KoenHoof

New member
Joined
Feb 3, 2020
Messages
3
Programming Experience
5-10
Dear all,

I think I have a simular problem. When creating a file and reading it using a OleDBConnection, I can not remove it afterwards. I am using conn.Close(), the 'using' statement, ... It keeps giving the 'file already in use' error when trying to delete it...

I placed a file "test.xlsx_copy" on my desktop and copied it, just to make sure the file is not used anywhere else... It also does also not matter where I put the file (temp folder, desktop, ...), it keeps giving the error. I am not using any antivirus, the windows antivirus is turned off.... I have no idea what can cause this problem and how to fix it...

C#:
            string filename = "C:\\Users\\Koen\\Desktop\\test.xlsx";
            File.Copy(filename + "_copy", filename);
            string strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", filename);
            using (OleDbConnection SQLConn = new OleDbConnection(strConnectionString))
            {
                SQLConn.Open();
                SQLConn.Close();
            }
            File.Delete(filename);

Anyone who can help me (or give an update on the previous problem)? I would be very gratefull!
 
What version of Office do you have?
Is it 32-bit or 64-bit?
Which version of the OLEDB ACE drivers did you install? (See the links to the drivers in this blogpost.)
 
I think I have a simular problem
Starting a new thread for yourself is the recommended action, although you could link to other similar thread and explain that you tried suggestions there and that didn't resolve your issue. Thread was split.
 
Last edited:
The following worked just fine for me. No exceptions thrown. (It was kind of unusual that calling SQLConn.Close() took about 4 seconds when I stepping with a debugger.

C#:
using System;
using System.IO;
using System.Data.OleDb;
using System.Data;

namespace SimpleCS
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = "Test.xlsx";
            File.Copy(filename + "_copy", filename);
            string strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", filename);
            using (OleDbConnection SQLConn = new OleDbConnection(strConnectionString))
            {
                SQLConn.Open();
                SQLConn.Close();
            }
            File.Delete(filename);
        }
    }
}

To avoid having to hard code a path, I setup "Test.xlsx_copy" to always copy into the "bin\Debug" or "bin\Release" directory as you can see from the Properties below.

Screenshot_1.png


As I said on the other thread, I'm using 64-bit Office 2016 Professional on Win10 (Version 1909) with Avira as my AV. A had to install the 64-bit 2016 ACE OLEDB drivers from Microsoft to be able to open that connection string.
 
The only way I could get the file is open exception was to explicitly open the file in Excel before line 17 is executed. In this case, though, the exception is absolutely correct because I do have the file open.
Screenshot_2.png
 
Last edited:
Hi all,

The fact that you guys can not reproduce the problem is as expected. I have used my software for more than 3 years, no problem at all. Last week it suddenly began throwing exceptions on the File.Delete. The only thing that changed is a simple windows update for office...

It just is not logical for the Connection.Close() to not close the connection, but that is the only possible reason it throws this exception. The file was newly created only a few lines above, no other proces is using the file (I checked) and I used the 'using' statement and close the connection explicitly

Trying to solve this issue, it hit me that maybe reinstalling the database engine can resolve this error and hurray (!), it did!
So apparently something fishy happend with the windows update, breaking the microsoft database engine... Reinstall it and all is well!
 
Last edited:
Back
Top Bottom