我想检查文件夹是否存在,如果不存在则创建它。但我不知道提供的路径是否有效。当路径无效时,会发生以下情况。
string path = "this is an invalid path";
if (!Directory.Exists(path))
Directory.CreateDirectory(path); //Exception thrown here
如果您提供无效路径,它将抛出DirectoryNotFoundException
例外。
我怎样才能阻止这种异常的发生?我不想使用 try-catch。我想在异常发生之前检测到该异常是否会发生。
Use 目录.存在 http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx检查文件夹是否存在的方法
if(Directory.Exists(path))
{
//Directory exists
}
else
{
// doesn't exist
}
请记住包括System.IO;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)