检查c#中路径中是否存在文件夹?
如何检查目录中是否存在名为RM
的文件夹…我通过文本框给出了目录路径,如txtBoxInput.Text
,在此路径中我必须检查…有什么建议吗?
Path.Combine和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 }
您可以使用Directory.Exists()
来测试某个文件夹是否在特定时刻存在,但请谨慎使用它! 如果您执行以下操作:
if (Directory.Exists(path)) { // Uh-oh! Race condition here! // Do something in path }
你陷入了经典的错误。 完全有可能在Directory.Exists()
调用和// Do something in path
,用户将删除该目录。 无论如何, 每当你进行文件I / O时,你必须处理在无法访问,不存在等情况下抛出的exception。如果你必须处理所有错误,它通常不是值得努力在顶部进行额外的,多余的检查。
using System.IO; if (Directory.Exists(path)) { // Do your stuff }
String Path = txtBoxInput.Text +’//’+“RM”;
if (Directory.Exists(path)) return true;