繁体   English   中英

检查c#中路径中是否存在文件夹?

[英]Check whether a folder exists in a path in 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时,你必须处理如果某些东西不可访问,不存在等引起抛出的异常。如果你必须处理所有错误,它往往不是值得努力在顶部放置额外的,多余的支票。

String Path = txtBoxInput.Text +'//'+“RM”;

 if (Directory.Exists(path))
return true;
using System.IO;


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM