在Windows上通过其路径强制关闭文件 [英] Force close file by its path on Windows

查看:51
本文介绍了在Windows上通过其路径强制关闭文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为其他开发人员编写一个临时文件管理器.我想删除文件,即使我们的控制台应用程序崩溃或被"X"按钮关闭.

I'm writing a temporary file manager for other developers. I want to remove files even our console applications crashes or being closed by "X" button.

到目前为止,我发现了 std :: set_terminate std :: atexit SetConsoleCtrlHandler 方法,可以删除所有需要的临时文件.问题是-我无法删除打开的文件.此外-我无法控制这些文件的流,导致开发人员使用多个库(例如GDAL),这些库使用自己的流机制,并且只能接受目标文件路径.

So far I found std::set_terminate, std::atexit and SetConsoleCtrlHandler methods with which I can delete all temporary files I need. Problem is - I can't delete opened files. Furthermore - I cant control streams to those files, cause developers using several libraries (GDAL for example) which uses their own stream mechanisms and can accept only a target file path.

如何强制关闭和删除当前应用程序打开的所有文件?

How can I force close and delete all files, opened by current application?

推荐答案

毕竟,我决定使用IInspectable的解决方案,他将其发布在评论中.只是将其发布为答案,以便我可以对其进行标记

After all I decieded to use IInspectable's solution, which he posts in comments. Just posting it as answer, so I could mark it

编写一个启动器应用程序,进行所有清理.这启动器在进程上调用CreateProcess和WaitForSingleObject处理.不管过程如何进行,都会向过程句柄发出信号终止(干净关闭,崩溃等).你不用担心关于强制关闭文件-操作系统关闭由一个文件保存的所有句柄在流程终止时进行处理.

Write a launcher application that does all the cleaning up. The launcher calls CreateProcess and WaitForSingleObject on the process handle. The process handle is signaled, regardless of how the process terminates (clean shutdown, crash, etc.). You don't have to worry about force closing files either - the OS closes all handles held by a process upon process termination.

这篇关于在Windows上通过其路径强制关闭文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆