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

后端 未结 4 1460
南旧
南旧 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:25

    String Path=txtBoxInput.Text+'//'+"RM";

     if (Directory.Exists(path))
    return true;
    
    0 讨论(0)
  • 2021-01-18 16:40
    using System.IO;
    
    
    if (Directory.Exists(path))
    {
         // Do your stuff
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-18 16:46

    Path.Combine and Directory.Exists ?

    http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx

    http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx

    if (Directory.Exists(Path.Combine(txtBoxInput.Text, "RM"))
    {
        // Do Stuff
    }
    
    0 讨论(0)
提交回复
热议问题