如何检查一个名为
RM
的文件夹是否存在于目录中...我已经通过文本框提供了目录路径,例如txtBoxInput.Text
,我需要在这个路径中进行检查...有什么建议吗?Path.Combine和Directory.Exists有什么区别?
http://msdn.microsoft.com/zh-cn/library/system.io.path.combine.aspx
http://msdn.microsoft.com/zh-cn/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时,必须处理抛出的异常,如果某些内容不可访问、不存在等等。如果您必须处理所有错误,那么在顶部添加额外的多余检查通常是不值得的努力。
using System.IO;
if (Directory.Exists(path))
{
// Do your stuff
}
字符串路径=txtBoxInput.Text+'//'+"RM";
if (Directory.Exists(path))
return true;