c# - Directory.Exists (Dir) 功能未按预期工作

标签 c# directoryinfo

在我的代码中,我需要检查目录是否存在,如果不存在,则创建它。

不幸的是,即使 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/

相关文章:

C# 线程处理 Parallel.ForEach 和 DirectoryInfo 内存不足异常

C# - 如何使用正则表达式替换 NULL 字符?

c# - 如何从c++项目中调用用c#创建的dll文件?

c# - 执行文件上传时出错

vb.net - 搜索单词并找到它的行号 LINQ

c# - 使用 LINQ 过滤 DirectoryInfo 的 ObservableCollection

c# - 删除自定义 IComparable 类中的重复项

c# - AutoMapper 映射到基于枚举的不同类型?

c# - 目录信息枚举

C# 访问路径 'C:\Documents and Settings\' 被拒绝