在我的代码中,我需要检查目录是否存在,如果不存在,则创建它。
不幸的是,即使 Directory.Exists
返回 false,目录存在并抛出 execption。
这是我正在使用的代码:
if (!Directory.Exists(emailAttachmentsPath))
{
Directory.CreateDirectory(emailAttachmentsPath);
}
即使该目录存在于磁盘上,该函数也会返回 false 并且出现错误。
有什么我想念的吗?
最佳答案
猜一猜,确定emailAttachmentsPath
不包含文件名。见 doc这里:
Return Value Type: System.Boolean true if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified file exists. true if path refers to an existing directory; otherwise, false.
关于c# - Directory.Exists (Dir) 功能未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22983942/