在代码中 - 删除包含文件的文件夹

发布于 2025-01-06 08:56:30 字数 713 浏览 0 评论 0原文

我正在尝试编写代码来删除其中包含文件的文件夹。我尝试了几种方法,但每次都会遇到同样的问题。我删除了文件夹中的文件,文件夹看起来很好。但似乎对该文件夹的某种引用仍然存在。

我单步执行我的代码,找到删除函数,它们似乎运行良好。然后我转到 Windows 资源管理器并尝试访问我刚刚删除的文件夹。尽管代码说它已删除它,但它仍然在那里,但是当我单击它时,它显示“访问被拒绝”。我尝试的方法之一确实从 Windows 资源管理器中删除了该文件夹,但是当我尝试创建具有相同名称和位置的文件夹时,我收到错误,并且 GetLastError 中显示 E_ACCESSDENIED。

那么我需要做什么才能正确删除文件夹吗?

我使用的一种方法是使用 SHFileOperation。另一种方法是使用FindFirstData并删除所有文件,然后使用RemoveDirectory删除空文件夹。两者都导致我遇到访问被拒绝的问题。

编辑:这是我的 SHFileOperation。 https://stackoverflow.com/ questions/9291995/cant-get-shfileoperation-to-delete-a-directory-with-more-multi-one-file

Im trying to write code that will delete a folder that has files in it. Ive tried a couple of ways and i get the same problem each time. I delete the files in the folder and the folder seemingly fine. But it seems some sort of reference to the folder still exists.

I step thorugh my code and i get to the delete functions(s) and they seem to pass fine. Then I go to Windows Explorer and try to access the folder i just deleted. It is still there even though the code says it deleted it, but when i click on it it says "Access Denied". One of the methods I tried did remove the folder from windows explorer, but when i try to create a folder with the same name and location I get an error and from GetLastError it says E_ACCESSDENIED.

So is there something i have to do to properly delete a folder?

One method I used was using SHFileOperation. Another was to use FindFirstData and delete all the files then use RemoveDirectory to delete the empty folder. Both lead me to this Access Denied problem.

EDIT: Here is my SHFileOperation. https://stackoverflow.com/questions/9291995/cant-get-shfileoperation-to-delete-a-directory-with-more-than-one-file

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

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

发布评论

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

评论(3

谈场末日恋爱 2025-01-13 08:56:30

您可以使用 Boost.FileSystem

在您的情况下,

boost::filesystem::remove_all(yourPath)

这将删除路径中的所有文件。然后使用:

RemoveDirectory( LPCTSTR lpPathName );    // Windows only

You can use Boost.FileSystem.

In your case that would be

boost::filesystem::remove_all(yourPath)

This will remove all the files in the path. Then use:

RemoveDirectory( LPCTSTR lpPathName );    // Windows only
痕至 2025-01-13 08:56:30

您可能使用

system ( "RD /S /Q [path] " );

效果很好,但会永久删除文件夹中的所有内容。
所以使用前先备份!!

You may use

system ( "RD /S /Q [path] " );

works well but deletes everything in the folder permanently.
So backup before u use it!!

十年九夏 2025-01-13 08:56:30

查看我用 C++ 编写的 remove_folder() 这里,我回答了与你类似的问题。它不使用任何第三方库,例如 boost。

Check out the remove_folder() I wrote in C++ here, which I answered a similar question as yours. It doesn't use any 3rd-party libraries like boost.

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