简体   繁体   English

关闭.Net Winforms应用程序并删除可执行文件的最佳方法是什么

[英]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: 只要可执行文件中的任何代码都在内存中,就无法将其删除,但是您可以尝试以下其他操作:

  • Telling MoveFileEx to defer the delete until next reboot is one approach typically used by install/uninstall code. 告诉MoveFileEx将删除推迟到下次重新启动时,这是安装/卸载代码通常使用的一种方法。
  • Use the Task Scheduler to schedule 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秒,然后立即退出以使其成功。
  • A batch file works well and can actually delete itself because a batch file is closed between lines. 批处理文件运行良好,并且实际上可以删除自身,因为批处理文件在行之间关闭。

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 可能的方法

  1. Launch a script to delete the application on exit, although this may leave the script lying around 启动脚本以在退出时删除应用程序,尽管这可能会使脚本无处不在

  2. 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.

相关问题 在WinForms应用程序中关闭套接字服务器的正确方法? - Proper way to shut down socket server in WinForms application? .NET winforms应用程序在不使用ClickOnce的情况下更新自身的最佳方法是什么? - What's the best way for a .NET winforms application to update itself without using ClickOnce? .NET Windows窗体应用程序更新自身的最佳方法是什么? - What's the best way for a .NET windows forms application to update itself? 让 WinForms 应用程序永久运行的最佳方式是什么 - What is the best way to have a WinForms application running eternally 在Web服务器上部署可执行进程的最佳方法是什么? - What's the best way to deploy an executable process on a web server? 一段时间后关闭程序的最佳方法 - Best way to shut down program after certain time 获取winforms应用程序名称的正确方法是什么? - what is the right way of getting my winforms application's name? 监视桌面应用程序的最佳方法是什么? - What's the best way to watchdog a desktop application? 如何在.NET中检测哪个线程阻止应用程序关闭 - How to detect which thread is holding the application from shut down in .NET 如何通知正在运行的线程该应用程序将在.net中关闭? - How to notify the running thread that application is going to shut down in .net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM