C++ Win32 API Delete file with progress bar

坚强是说给别人听的谎言 提交于 2019-12-11 11:55:25

问题


Using the windows api, is there any way to delete a large file (lets say 1gb+), and monitor the progress somehow? I'm sure its possible but I have no idea where to start..

EDIT: Should have been more specific, I want to move the file to the recycle bin, and show a progress bar similar to explores, though I might want the progress bar in a console or something so I don't want an exact replica.

EDIT 2: Yeaah guess it is instant, should have tested before I asked the question. Anyway to just close this question?


回答1:


Use SHFileOperation with the FO_DELETE func and FOF_ALLOWUNDO flag to move a file to the Recycle Bin. Progress will automatically be shown unless you also specify FOF_SILENT.

SHFILEOPSTRUCT fileop = { 0 };
fileop.hwnd = hwndMain; /* your window */
fileop.wFunc = FO_DELETE;
fileop.pFrom = szFilePathToDelete;
fileop.fFlags = FOF_ALLOWUNDO /* | FOF_NOCONFIRMATION to recycle without prompting */;
int error = SHFileOperation(&fileop);

Update: As noted in the question edit, progress won't be shown for a single file, but it will be shown if recycling an entire directory. This also doesn't let you override the UI (e.g., to display progress in a console window).




回答2:


Perhaps you could progressively truncate it using SetEndOfFile? http://msdn.microsoft.com/en-us/library/aa365531(v=vs.85).aspx. Then removing the inode (or whatever it's called in Windows-land) could be quickly done.

[update]Just tested; the other guys are right. Removing the inode (delete) is instantaneous on a 1gb file.



来源:https://stackoverflow.com/questions/6339348/c-win32-api-delete-file-with-progress-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!