Check whether a folder exists in a path in c#?

后端 未结 4 1503
南旧
南旧 2021-01-18 16:19

How to check whether a folder named RM exists inside a directory...I have given the directory path through textbox like txtBoxInput.Text and in thi

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-18 16:41

    You can use Directory.Exists() to test whether a folder exists at a particular moment in time, but use it with caution! If you do something like:

    if (Directory.Exists(path))
    {
        // Uh-oh!  Race condition here!
        // Do something in path
    }
    

    you've fallen into a classic blunder. It's entirely possible that, between the Directory.Exists() call and the // Do something in path, a user will have deleted the directory. No matter what, whenever you do file I/O, you must handle the exceptions that get thrown if something isn't accessible, doesn't exist, etc. And if you have to handle all the errors anyway, it frequently isn't worth the effort to put an additional, superfluous check at the top.

提交回复
热议问题