Directory.CreateDirectory 不创建文件夹

Directory.CreateDirectory does not create a folder

我正在尝试使用 Directory.CreateDirectory() 创建一个新目录,但我无法理解为什么当我使用以下代码时它没有创建一个新目录。

var directory = Path.Combine(Environment.CurrentDirectory, "Status" + "-" + "Test" + Guid.NewGuid() + "\");
Directory.CreateDirectory(directory);

但是当我手动输入文件路径时,它会创建目录(效果很好)。

Directory.CreateDirectory(@"F:\Code\Help\");

我做错了吗?

非常感谢任何意见或反馈。 TIA.

你的代码本身没有任何问题

我怀疑你正在创建目录(某处)

try
{
   var directory = Path.Combine(Environment.CurrentDirectory, $@"Status-Test{Guid.NewGuid()}");
   Console.WriteLine(directory);

   var di = Directory.CreateDirectory(directory);

   Console.WriteLine($"The directory was created successfully at {Directory.GetCreationTime(directory)}.");
   Console.WriteLine($"==> { di.FullName}");
}
catch (Exception e)
{
   Console.WriteLine("Oh NOES!: {0}", e);
}

Environment.CurrentDirectory Property

By definition, if this process starts in the root directory of a local or network drive, the value of this property is the drive name followed by a trailing slash (for example, "C:\"). If this process starts in a subdirectory, the value of this property is the drive and subdirectory path, without a trailing slash (for example, "C:\mySubDirectory").