简体   繁体   English

C++ Win32 API 删除文件带进度条

[英]C++ Win32 API Delete file with progress bar

Using the windows api, is there any way to delete a large file (lets say 1gb+), and monitor the progress somehow?使用 windows api,有没有办法删除一个大文件(比如说 1gb+),并以某种方式监控进度? 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.编辑 2:是的,我猜它是即时的,在我问这个问题之前应该已经测试过了。 Anyway to just close this question?无论如何要关闭这个问题?

Use SHFileOperation with the FO_DELETE func and FOF_ALLOWUNDO flag to move a file to the Recycle Bin.使用带有FO_DELETE函数和FOF_ALLOWUNDO标志的SHFileOperation将文件移动到回收站。 Progress will automatically be shown unless you also specify FOF_SILENT.除非您还指定 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 (eg, to display progress in a console window).这也不允许您覆盖 UI(例如,在控制台窗口中显示进度)。

Perhaps you could progressively truncate it using SetEndOfFile?也许您可以使用 SetEndOfFile 逐步截断它? http://msdn.microsoft.com/en-us/library/aa365531(v=vs.85).aspx . 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.然后可以快速完成删除 inode(或在 Windows 领域中调用的任何内容)。

[update]Just tested; [更新]刚刚测试过; the other guys are right.其他人是对的。 Removing the inode (delete) is instantaneous on a 1gb file.删除 inode(删除)在 1gb 文件上是即时的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM