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

ぃ、小莉子 提交于 2019-12-19 19:45:35

问题


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 this path i have to check...Any suggestion?


回答1:


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
}



回答2:


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.




回答3:


using System.IO;


if (Directory.Exists(path))
{
     // Do your stuff
}



回答4:


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

 if (Directory.Exists(path))
return true;


来源:https://stackoverflow.com/questions/4509415/check-whether-a-folder-exists-in-a-path-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!