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.