CreateFolder only shoudl succeed if folder doesnt exist

I was looking for a method in .NET similar to MkDir in VB the io.Directory.CreateDirectory() method succeeds even if the folder that i am trying to create already exists.

I am looking for a method that should potentially error out while trying to create a directory if it already exist.

any ideas




Answer this question

CreateFolder only shoudl succeed if folder doesnt exist

  • Rob1

    The CreateDirectory method does throw an exception when the directory exists, but then again, u can make it a function and therefor call it by multiple threads. Directory.Exists does all the work.

    Maybe this is not what you really mean but i can't think of an other solution.



  • Ardeur

    U can use Directory.Exists(string path);

    like this:

    if(!Directory.Exists(path))
    {
         DirectoryInfo newDirectory = Directory.CreateDirectory(path);
    }

     

    where path is the path to check...



  • Tmac

    since the application is multi threaded, the other thread might create the folder after the check is done by one thread so i was method thas is reliable even if multiple thread is executed.

    I was expecting that createDirectory will fail if directory already exist or atleast there is an overloaded CreateDirectory method that could be used.



  • CreateFolder only shoudl succeed if folder doesnt exist