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?
The Directory.CreateDirectory
call 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.CreateDirectory
in 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 occur
CreateDirectory
for c:\temp\foo
and it succeedsc:\temp
from program 1 user CreateDirectory
and 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.