gpt4 book ai didi

c# - Directory.CreateDirectory 是异步的还是什么?

转载 作者:行者123 更新时间:2023-11-30 12:56:42 24 4
gpt4 key购买 nike

我有以下代码来为我生成的文件准备一个文件夹:

if (Directory.Exists(outputDir))
{
Directory.Delete(outputDir, true);
}
Directory.CreateDirectory(outputDir);

当我正常运行它时,恰好每隔一秒它就工作一次,而另一次它抛出一个 DirectoryNotFoundException,在线:

File.WriteAllLines(filePath, CreateRows(TestLineCount, TestSampleCount));

找不到路径的一部分 'C:\Dev\Android\Projects...filePath 中的最后一个文件夹丢失了,因为我删除了它,但后来我又创建了它,所以它应该一直在那里。当我在 Directory.CreateDirectory(outputDir); 行上放置一个断点时,有一个小的延迟,应用程序每次都能正常工作。如果我引入自动延迟,即:

Directory.CreateDirectory(outputDir);
Thread.Sleep(500);

它也每次都有效。当然所有这些调用都应该阻塞直到完成,所以 outputDir 总是在那里?

最佳答案

我认为它不是异步的,但是当你这样调用它时

Directory.CreateDirectory(outputDir); //App is trying to create directory
//successfully created but depends on OS how fast OS commits the changes

Thread.Sleep(500); //assuming physically creation on Hard and its availability in OS took less then 500ms then this wait works or else we have to wait more

//this delay is actually totally non generic since the time differs OS to OS


// its just a rough guess that changes will be committed within 500ms

if(!Directory.Exists(outputDir))
//if still not exists Your OS que is being processed very slow due to
//some not known reason! so you have to wait more then 500ms

关于c# - Directory.CreateDirectory 是异步的还是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39303525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com