Can I safely attempt to create the same directory from two different threads, without having one of them throw an exception, or run into other issues?
Note that according to MSDN, it is OK to call CreateDirectory() on a directory which already exists, in which case the method is expected to do nothing.
The
Directory.CreateDirectorycall itself is safe to make from multiple threads. It will not corrupt program or file system state if you do so.However it’s not possible to call
Directory.CreateDirectoryin such a way to guarantee it won’t throw an exception. The file system is an unpredictable beast which can be changed by other programs outside your control at any given time. It’s very possible for example to see the following occurCreateDirectoryforc:\temp\fooand it succeedsc:\tempfrom program 1 userCreateDirectoryand throws due to insufficient accessIn short you must assume that
Directory.CreateDirectory, or really any function which touches the file system, can and will throw and handle accordingly.