[英]What's the best way to shut down a .Net winforms application and delete the executable
I need to shut down my application and then delete the executable. 我需要关闭我的应用程序,然后删除可执行文件。 I am doing it by kicking off a low priority batch job and then immediately shutting down the exe.
我通过启动低优先级批处理作业然后立即关闭exe来完成此操作。 Is there a better way?
有没有更好的办法?
There are the PendMoves and MoveFile utilities from sysinternals . sysinternals提供了PendMoves和MoveFile实用程序。 According to the documentation, you can use the movefile to delete a file on the next bootup by specifying an empty destination:
根据文档,您可以在下一次启动时使用movefile来删除文件,方法是指定一个空目标:
movefile your.exe ""
This utility is commonly used to remove stubborn malware, but I'm not sure if it will immediately delete your application after it closes or only on the next reboot. 该实用程序通常用于删除顽固的恶意软件,但是我不确定它是否会在关闭后还是仅在下次重新启动后立即删除您的应用程序。
The utility uses the MoveFileEx API , so if you want you can use that API to achieve the same effect. 该实用程序使用MoveFileEx API ,因此,如果需要,可以使用该API达到相同的效果。
MoveFileEx will work as long as you aren't on Windows 95/98/ME (I hope not...). 只要您不在Windows 95/98 / ME上,MoveFileEx都可以正常工作(我希望不要...)。 But, you can't delete the folder your exe was in. If that's not a problem, follow Ryan's advice.
但是,您无法删除exe所在的文件夹。如果那不是问题,请遵循Ryan的建议。
This article chronicles basically every way to do what you want. 本文基本上记录了您想要做的所有方法。
As long as any code from an executable is in memory, it cannot be deleted, but there are some other things you can try: 只要可执行文件中的任何代码都在内存中,就无法将其删除,但是您可以尝试以下其他操作:
MoveFileEx
to defer the delete until next reboot is one approach typically used by install/uninstall code. MoveFileEx
将删除推迟到下次重新启动时,这是安装/卸载代码通常使用的一种方法。 cmd.exe /c del c:\\path\\myprog.exe
to run 60 seconds from now, then quit promptly so it has a chance of succeeding. cmd.exe /c del c:\\path\\myprog.exe
计划从现在开始运行60秒,然后立即退出以使其成功。 If you're using the batch file method, consider something like the following: 如果使用批处理文件方法,请考虑以下内容:
:loop
ping -n 6 127.0.0.1
del /y %1
if exist %1 goto :loop
del byebye.bat
The ping
command here is being abused to insert a delay, since sleep
is not a standard command on Windows. 由于
sleep
不是Windows上的标准命令,因此此处的ping
命令被滥用以插入延迟。 The loop enables the batch process to bide its time until it can actually remove the executable. 该循环使批处理过程可以花费时间,直到它可以实际删除可执行文件为止。
Possible approaches 可能的方法
Launch a script to delete the application on exit, although this may leave the script lying around 启动脚本以在退出时删除应用程序,尽管这可能会使脚本无处不在
Use a RunOnce key to have the executable deleted next time the machine restarts, i think it applies to logon and logoff as well but i'm not certain. 使用RunOnce密钥在下次计算机重新启动时删除可执行文件,我认为它也适用于登录和注销,但我不确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.