The standard .NET Directory.CreateDirectory(string path) method creates a directory (including parent directories, if needed), if they don't already exists, and returns a DirectoryInfo object for the created directory.
And if the directory does already exist, it returns a DirectoryInfo object for the directory that was already there.
I need a method that will create a directory, if it doesn't already exist, but will fail - returning an error or throwing an exception - if the directory already exists.
And no, Directory.Exists(string path) will not serve. I need something that is atomic. I'm interfacing with C++ apps that were designed to the the presence or absence of certain directories as locks, and doing DirectoryExists() followed by Directory.CreateDirectory() leaves a window that can be stepped on.
At the filesystem level, creating a directory is an atomic operation. If two processes both attempt it, only one will succeed. And the API available to C++ programs reflects this. The standard API in .NET hides this. Is there some lower-level API that is available that does not?