File Copy Locks File So Data Cannot Be Written

43 Views Asked by At

I have a couple of file templates stored in C:\Webs\web-n-rsgpsd\Administration\Templates as part of a deployed web application. Data should then be exported to a copy of the file (copied from Templates to ExtractOutput), but each time it runs, the application errors with a 500 - Internal Server Error and according to the application log on the server:
The process cannot access the file 'C:\Webs\web-n-rsgpsd\Administration\ExtractOutput\AskDataExtractTemplate.xlsx' because it is being used by another process.0
So whilst the file is copied (and can be seen in the ExtractOutput folder, the data can't be written to it. If I attempt to open the file from the ExtractOutput folder, it says the file is locked for editing by 'another user'.
Here is the code that creates the copy of the template:

    private FileInfo GetExportFilename()
    {
        const string templateFilename = "AskDataExtractTemplate.xlsx";
        var templateFileInfo = new FileInfo(Server.MapPath($"~/Administration/Templates/{templateFilename}"));
        var temporaryFile = Server.MapPath($"~/Administration/ExtractOutput/{templateFilename}");
        templateFileInfo.CopyTo(temporaryFile, true);
        templateFileInfo = new FileInfo(temporaryFile) { IsReadOnly = false };
        return templateFileInfo;
    }

This is the connection string it is then used with to export data:
Connection string:Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Webs\web-n-rsgpsd\Administration\ExtractOutput\ASKDataExtractTemplate.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES'
Can anyone shed any light on why the file is locked, and therefore the data can't be written to it?

0

There are 0 best solutions below