views:

46

answers:

1

Sometimes, no "foo" directory is left after running this code:

string folder = Path.Combine(Path.GetTempPath(), "foo");
if (!Directory.Exists(folder))
    Directory.CreateDirectory(folder);
Process.Start(@"c:\windows\explorer.exe", folder);
Thread.Sleep(TimeSpan.FromSeconds(5));
Directory.Delete(folder, false);
Directory.CreateDirectory(folder);

It seems Windows Explorer keeps a reference to the folder, so the last CreateDirectory has nothing to do, but then the original folder is deleted. How can I fix the code?

EDIT: I'm sorry that my question wasn't clear. My objective is to create an empty "foo" directory. If the directory already exists, I delete it and create it anew. The problem is that if Windows Explorer is looking at the directory, the CreateDirectory call sometimes fails silently. No exception is raised; the directory just isn't created.

The code above reproduces the issue in my computer. Only the last two lines belong to my actual application. The previous lines are setup. After you run the code, does "foo" always exist? This is not the case half the time in my pc.

For the time being, I'm manually deleting each file and subdirectory of foo.

+1  A: 

Since you did not provide details (like exceptions, errors), I will assume this is the problem.

I think the problem is that the explorer is still running when you run the command to delete the folder. This could be a locking problem.

Directory.Delete(folder, false);

Either that, or there is some other application accessing that folder or its sub-folders or files, if any by chance.

I would recommend not touching the folder via explorer or any other app, if possible, and wait for the explorer to exit first, before deleting the folder.

        Process p = Process.Start(@"c:\windows\explorer.exe", folder);
        Thread.Sleep(TimeSpan.FromSeconds(5));
        p.WaitForExit(); //<-------
        Directory.Delete(folder, false);

Also, please do not ignore the exceptions and errors thrown and paste them here, if any.

Hope it helps.

Nayan
I've clarified the question and hope it makes more sense now.
Bruno Martinez
My answer still remains the same. Your folder is not deleted because some app is still accessing it, hence the locking. Once the locking apps are gone, the folder is free to be deleted. The locking app could be your explorer or even as unsuspecting as your Anti-Virus! You can maybe delay the deletion for few milliseconds and try again. Or if you really want to go deep, find the locking application and kill it. Try searching for source code of `WhoSLocking` to understand how to find locking apps.
Nayan
The folder IS deleted. The bug is that it's not created. I think that delete-create are not executed in that order. The delete stalls because of Windows Explorer, then the create runs but does nothing since the directory exists, finally the directory is deleted. I find it very puzzling that the OS doesn't seem to present a consistent view of the File system to my process.
Bruno Martinez
Can you **please provide the exceptions** that you get during this process? If there are none, would you like to put a break-point at `CreateDirectory`, and before continuing, check if the folder is actually deleted or not. Then step over the `CreateDirectory` and check if the folder is created or not. These will help in understanding what is happening. Again: do not forget to post the exception(s) that you get.
Nayan
I get no exception whatsoever. Have you run the code? Does the directory always exist after you run the code?
Bruno Martinez
Hmm.. it seems that I get exception in VS 2010 if I create folder in foo directory before starting the prog.`System.IO.IOException was unhandled - Message=The directory is not empty.`I recommend you to devise some test cases and run them. You can post them here if you like to verify their intelligence. =)
Nayan