我怎样才能让我的.net应用程序清除本身?我的、应用程序、net

2023-09-02 02:07:23 作者:风月斟酒

我怎样才能让我的C#应用​​程序删除本身(自爆)?这里有两种方式,我认为可能的工作:

How can I make my C# app erase itself (self-destruct)? Here's two ways that I think might work:

供应另一个程序,删除主程序。这个删除器程序是如何删除,那么有关系吗? 创建一个进程,CMD的等待几秒钟,然后删除你的文件。在那几秒钟,你关闭你的应用程序。

两种这些方法显得效率很低。我有一种感觉,那就是在Windows中一些内置的标志或东西,让这样的东西。我应该怎么办呢?另外,还可以提供一些样品code?

Both of those methods seem inefficient. I have a feeling that there's some built-in flag or something in Windows that allows for such stuff. How should I do it? Also, can you provide some sample code?

更新:谢谢你的答案!我要去尝试一下,看看那里的得到我。

UPDATE: Thanks for all your answers! I'm going to try them, and see where that gets me.

首先,有些人问,为什么我会想我的应用程序做到这一点。这里的答案:前几天,我读工程土豚规范乔尔Spolsky的贴在他的博客,并提到,客户端应用程序将远程会议结束后删除自身。我不知道这是如何工作的,怎么样,如果我需要做到这一点,我可以完成这样的壮举。

First of all, some people have asked why I'd want my app to do this. Here's the answer: a few days ago, I read the Project Aardvark spec that Joel Spolsky posted on his blog, and it mentioned that the client app would delete itself after the remote session. I'm wondering how this works, and how, if I ever need to do this, I can accomplish such a feat.

下面是一个什么样的建议了一个小概述:

Here's a little overview of what's been suggested:

创建注册表项告诉Windows删除重新启动文件 启动CMD用ping命令,等待几秒钟,然后删除该文件 Create a registry entry that tells Windows to delete the file on reboot Launch CMD with a ping command to wait a few seconds and then delete the file

这两项的,当然,也有缺点,如在评论中概述。

Both of those, of course, have their disadvantages, as outlined in the comments.

然而,将概述如下工作这样的方法?

However, would such a method as outlined below work?

有两个可执行文件:Program.exe文件和Cleaner.exe。前者是程序本身,后者是删除Program.exe文件和它本身(如果它加载到内存中,因为我即将解释)的应用程序。是否有可能为Program.exe文件(具有相关性)为负荷所有Cleaner.exe,对不具有任何相关性,到内存并运行它?

There are two executables: Program.exe and Cleaner.exe. The former is the program itself, the latter is the app that deletes Program.exe and itself (if it's loaded into memory, as I'm about to explain). Is it possible for Program.exe (which has dependencies) to load all of Cleaner.exe, which doesn't have any dependencies, into memory and run it?

如果这是可能的,可以Cleaner.exe内Program.exe文件打包,加载到内存中,并运行?

If this is possible, could Cleaner.exe be packaged inside Program.exe, loaded into memory, and run?

推荐答案

有一个的 MoveFileEx API,其中,当给定一个 MOVEFILE_DELAY_UNTIL_REBOOT 标记,将删除系统下次启动指定的文件。

There's a MoveFileEx API, which, when given a MOVEFILE_DELAY_UNTIL_REBOOT flag, will delete specified file on next system startup.