在代码中 - 删除包含文件的文件夹
我正在尝试编写代码来删除其中包含文件的文件夹。我尝试了几种方法,但每次都会遇到同样的问题。我删除了文件夹中的文件,文件夹看起来很好。但似乎对该文件夹的某种引用仍然存在。
我单步执行我的代码,找到删除函数,它们似乎运行良好。然后我转到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Boost.FileSystem。
在您的情况下,
这将删除路径中的所有文件。然后使用:
You can use Boost.FileSystem.
In your case that would be
This will remove all the files in the path. Then use:
您可能使用
效果很好,但会永久删除文件夹中的所有内容。
所以使用前先备份!!
You may use
works well but deletes everything in the folder permanently.
So backup before u use it!!
查看我用 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.