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

发布于 2024-07-14 00:10:09 字数 74 浏览 7 评论 0 原文

我需要关闭我的应用程序,然后删除可执行文件。 我通过启动低优先级批处理作业然后立即关闭 exe 来完成此操作。 有没有更好的办法?

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. Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花想c 2024-07-21 00:10:09

有来自 MoveFile 实用程序://www.sysinternals.com/" rel="nofollow noreferrer">sysinternals。 根据文档,您可以使用 movefile 通过指定空目标来在下次启动时删除文件:

movefile your.exe ""

此实用程序通常用于删除顽固的恶意软件,但我不确定它是否会在关闭后立即删除您的应用程序或者仅在下次重新启动时。

该实用程序使用 MoveFileEx API,因此如果您希望可以使用该 API 来达到相同的效果。

There are the PendMoves and MoveFile utilities from sysinternals. According to the documentation, you can use the movefile to delete a file on the next bootup by specifying an empty destination:

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.

野却迷人 2024-07-21 00:10:09

只要您不在 Windows 95/98/ME 上,MoveFileEx 就可以工作(我希望不是......)。 但是,您无法删除 exe 所在的文件夹。如果这不是问题,请遵循 Ryan 的建议。

文章 基本上记录了做你想做的事情的所有方法。

MoveFileEx will work as long as you aren't on Windows 95/98/ME (I hope not...). But, you can't delete the folder your exe was in. If that's not a problem, follow Ryan's advice.

This article chronicles basically every way to do what you want.

[浮城] 2024-07-21 00:10:09

只要可执行文件中的任何代码都在内存中,就无法删除它,但您可以尝试其他一些操作:

  • 告诉 MoveFileEx 将删除推迟到下次重新启动是安装/卸载代码通常使用的一种方法。
  • 使用任务计划程序来安排 cmd.exe /c del c:\path\myprog.exe 从现在起运行 60 秒,然后立即退出,以便有成功的机会。
  • 批处理文件运行良好,并且实际上可以删除自身,因为批处理文件在行之间是关闭的。

如果您使用批处理文件方法,请考虑如下内容:

:loop
ping -n 6 127.0.0.1
del /y %1
if exist %1 goto :loop
del byebye.bat

这里的 ping 命令被滥用来插入延迟,因为 sleep 不是标准命令视窗。 该循环使批处理能够等待时间,直到它实际上可以删除可执行文件。

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.
  • 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.
  • 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. The loop enables the batch process to bide its time until it can actually remove the executable.

怎会甘心 2024-07-21 00:10:09

可能的方法

  1. 启动脚本以在退出时删除应用程序,尽管这可能会留下脚本

  2. 使用RunOnce 键可在下次计算机重新启动时删除可执行文件,我认为它也适用于登录和注销,但我不确定。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文