我想检查一个文件夹是否存在,如果不存在则创建它。但我不知道提供的路径是否有效。当路径无效时,会发生以下情况。
string path = "this is an invalid path";
if (!Directory.Exists(path))
Directory.CreateDirectory(path); //Exception thrown here
如果您提供了无效路径,它将抛出一个
DirectoryNotFoundException
异常。我该如何阻止此异常的发生?我不想使用try-catch。我希望在异常发生之前就能检测到这个异常。