防止 Windows 资源管理器干扰目录操作

发布于 2024-08-30 01:06:15 字数 670 浏览 6 评论 0原文

有时,运行此代码后不会留下“foo”目录:

string folder = Path.Combine(Path.GetTempPath(), "foo");
if (!Directory.Exists(folder))
    Directory.CreateDirectory(folder);
Process.Start(@"c:\windows\explorer.exe", folder);
Thread.Sleep(TimeSpan.FromSeconds(5));
Directory.Delete(folder, false);
Directory.CreateDirectory(folder);

似乎Windows资源管理器保留了对该文件夹的引用,因此最后的CreateDirectory无关,但随后原始文件夹被删除。我该如何修复代码?

编辑: 很抱歉我的问题不清楚。我的目标是创建一个空的“foo”目录。如果该目录已存在,我将其删除并重新创建。问题是,如果 Windows 资源管理器正在查看目录,则 CreateDirectory 调用有时会默默失败。没有例外;该目录尚未创建。

上面的代码在我的计算机上重现了该问题。只有最后两行属于我的实际应用。前面几行已设置。运行代码后,“foo”是否始终存在?在我的电脑上,有一半的情况并非如此。

目前,我正在手动删除 foo 的每个文件和子目录。

Sometimes, no "foo" directory is left after running this code:

string folder = Path.Combine(Path.GetTempPath(), "foo");
if (!Directory.Exists(folder))
    Directory.CreateDirectory(folder);
Process.Start(@"c:\windows\explorer.exe", folder);
Thread.Sleep(TimeSpan.FromSeconds(5));
Directory.Delete(folder, false);
Directory.CreateDirectory(folder);

It seems Windows Explorer keeps a reference to the folder, so the last CreateDirectory has nothing to do, but then the original folder is deleted. How can I fix the code?

EDIT:
I'm sorry that my question wasn't clear. My objective is to create an empty "foo" directory. If the directory already exists, I delete it and create it anew. The problem is that if Windows Explorer is looking at the directory, the CreateDirectory call sometimes fails silently. No exception is raised; the directory just isn't created.

The code above reproduces the issue in my computer. Only the last two lines belong to my actual application. The previous lines are setup. After you run the code, does "foo" always exist? This is not the case half the time in my pc.

For the time being, I'm manually deleting each file and subdirectory of foo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错々过的事 2024-09-06 01:06:15

由于您没有提供详细信息(例如异常、错误),我认为这就是问题所在。

我认为问题在于,当您运行删除文件夹的命令时,资源管理器仍在运行。这可能是锁定问题。

Directory.Delete(folder, false);

要么是这样,要么有其他应用程序访问该文件夹或其子文件夹或文件(如果有的话)。

如果可能的话,我建议不要通过资源管理器或任何其他应用程序触摸该文件夹,并先等待资源管理器退出,然后再删除该文件夹。

        Process p = Process.Start(@"c:\windows\explorer.exe", folder);
        Thread.Sleep(TimeSpan.FromSeconds(5));
        p.WaitForExit(); //<-------
        Directory.Delete(folder, false);

此外,请不要忽略抛出的异常和错误,以及将它们粘贴到此处(如果有)。

希望有帮助。

Since you did not provide details (like exceptions, errors), I will assume this is the problem.

I think the problem is that the explorer is still running when you run the command to delete the folder. This could be a locking problem.

Directory.Delete(folder, false);

Either that, or there is some other application accessing that folder or its sub-folders or files, if any by chance.

I would recommend not touching the folder via explorer or any other app, if possible, and wait for the explorer to exit first, before deleting the folder.

        Process p = Process.Start(@"c:\windows\explorer.exe", folder);
        Thread.Sleep(TimeSpan.FromSeconds(5));
        p.WaitForExit(); //<-------
        Directory.Delete(folder, false);

Also, please do not ignore the exceptions and errors thrown and paste them here, if any.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文