IOException ("file or directory already exists") while trying to create a directory
Asked Answered
S

2

10

I have a strange problem in our C# project which occurs while trying to create a directory via IronPython script. This is the code:

targetTemplateDirectory = Path.Combine(Data, "Templates\\CheckedReports")

if not Directory.Exists(targetTemplateDirectory):
    Directory.CreateDirectory(targetTemplateDirectory)

The problem is an IOException telling me that it is not possible to create the folder "H:\ProductName\Data\Templates\CheckedReports" because a file or directory with the same name already exists.

According to MSDN the method Directory.CreateDirectory() does not throw any exception when the directory already exists.

I know that a file named "CheckedReports" can be the reason for this exception, but it is very, very unlikely that the customer has created that file manually. In addition to that there is no line of code which contains the word "CheckedReports" (besides the mentioned script). Moreover the application is used by a few thousand customers, the script executed on every machine and only one customer reported this issue.

Is there any possibility for this exception to occur other than a file with the same name? Maybe something related to permissions, removable media or network drives?

Sihon answered 9/1, 2013 at 13:52 Comment(2)
"but it is very, very unlikely" ... yet, given the information available, it is the most likely reason. Can't you just check (or let the customer check) if a file by that name exists?Arithmomancy
I wish I could :). The application version which this customer used has an error reporting system which does not allow any personal or contact information. We changed that in a later update. So we only got a message containing the stack trace and a machine id, nothing else.Sihon
D
3

We had the same, and in our case it was pretty clear it was a permissions issue. We were expecting the documented UnauthorizedAccessException, but that's not what we got.

In the stack we have Directory.CreateDirectory calling Directory.InternalCreateDirectory.

Inside it there is this note:

        //Note that InternalExists may fail due 
        // to Win32 ACL's preventing us from seeing a directory, and this
        // isn't threadsafe. 

There are more notes about it, that go deeper into that fact, the code may try to create the directory that's already there when it couldn't see it.

Doggish answered 13/5, 2013 at 13:0 Comment(2)
How were you able to go about resolving this issue?Kinematograph
I don't recall this from 10 years ago but it sounds a lot like the API just returned a different exception than expected for a permissions issue. My best guess is to check the permissions of the folder that contains the directory you want to create. Or if the directory happens to already exist, check its permissions.Doggish
K
9

Although it would be a bit of an overkill to have this only for one user, it should be possible to check if a file with that name exists.

FileInfo myFile = new FileInfo(targetTemplateDirectory);
if (myFile.Exists)
    myFile.Delete();

if (!Directory.Exists(targetTemplateDirectory))
    Directory.CreateDirectory(targetTemplateDirectory);

Probably this would solve the issue IF ofcourse the I/O exception was caused by a file having the same name. If it would be caused by the user because the "network name is not known", then I would not have a clue either.

Kinard answered 9/1, 2013 at 14:23 Comment(2)
Maybe it's a bit of paranoia and there is no other reason for this exception, but is fatal because it is thrown during the update process and it prevents the application from starting. I'll give your solution a try, at least it solves any failure caused by a file of the same name, no matter whether it is manually created by the customer or programmatically due to an error in our application, our setup or the interaction with another application.Sihon
I guess another way of dealing with it is to just ignore IOExceptions, but that might not be too pretty.Weld
D
3

We had the same, and in our case it was pretty clear it was a permissions issue. We were expecting the documented UnauthorizedAccessException, but that's not what we got.

In the stack we have Directory.CreateDirectory calling Directory.InternalCreateDirectory.

Inside it there is this note:

        //Note that InternalExists may fail due 
        // to Win32 ACL's preventing us from seeing a directory, and this
        // isn't threadsafe. 

There are more notes about it, that go deeper into that fact, the code may try to create the directory that's already there when it couldn't see it.

Doggish answered 13/5, 2013 at 13:0 Comment(2)
How were you able to go about resolving this issue?Kinematograph
I don't recall this from 10 years ago but it sounds a lot like the API just returned a different exception than expected for a permissions issue. My best guess is to check the permissions of the folder that contains the directory you want to create. Or if the directory happens to already exist, check its permissions.Doggish

© 2022 - 2024 — McMap. All rights reserved.