简体   繁体   English

程序如何删除自己的可执行文件

[英]How can a program delete its own executable

Is there any way that a running process can delete its own executable?有什么方法可以让正在运行的进程删除自己的可执行文件?

For example, I make a console application (single exe) and after doing some tasks it somehow deletes the exe file.例如,我制作了一个控制台应用程序(单个 exe),在完成一些任务后,它以某种方式删除了 exe 文件。

I have to send a single file to someone.我必须向某人发送一个文件。 And I want it deleted after it does its intended task.我希望它在完成预期任务后被删除。

Is there anyway to do it in Windows无论如何在Windows中可以做到这一点

One way to do this is to use the MoveFileEx function with the MOVEFILE_DELAY_UNTIL_REBOOT flag and a NULL destination.一种方法是使用带有MOVEFILE_DELAY_UNTIL_REBOOT标志和NULL目标的MoveFileEx函数。 According to the documentation, this:根据文档,这是:

registers the lpExistingFileName file to be deleted when the system restarts.注册系统重启时要删除的lpExistingFileName文件。 If lpExistingFileName refers to a directory, the system removes the directory at restart only if the directory is empty.如果lpExistingFileName引用一个目录,则系统仅在目录为空时才会在重新启动时删除该目录。

process.start("cmd /c ping localhost -n 3 > nul & del filepath")
exit

Explanation :解释 :

ping localhost -n 3

Adds a slight delay before executing del filepath .在执行del filepath之前增加一点延迟。 By the time it's triggered, your program has exited.当它被触发时,您的程序已经退出。

Replace process.start with whatever command your programming language uses to start programs with arguments.将 process.start 替换为您的编程语言用来启动带参数的程序的任何命令。

Replace filepath with the path to your exe.将文件路径替换为您的 exe 的路径。

Replace exit with the command for terminating your program.将 exit 替换为终止程序的命令。

=== ===

10yr anniverary edit, if this doesn't work, you must find a way to perform a "process.start" that starts a separate (external) process, not one subordinate to your original calling program: Python , Bash , C , ...... or search for a different language 10 周年编辑,如果这不起作用,您必须找到一种方法来执行“process.start”,它启动一个单独的(外部)进程,而不是从属于您的原始调用程序: PythonBashC 、.. .... 或 搜索其他语言

Replace the search in the catch all with your programming language and you will likely find a suitable guide for this essential step.用您的编程语言替换全部内容中的搜索,您可能会找到适合此基本步骤的指南。 Please take care to ignore superfluous information as every question may come with obscure specific details that are unrelated to you.请注意忽略多余的信息,因为每个问题都可能带有与您无关的晦涩的具体细节。

You can use windows scheduler to schedule a task to delete your program after X seconds.您可以使用 Windows 调度程序安排任务以在 X 秒后删除您的程序。

Command line: http://msdn.microsoft.com/en-us/library/bb736357%28VS.85%29.aspx命令行:http: //msdn.microsoft.com/en-us/library/bb736357%28VS.85%29.aspx

Or API: http://msdn.microsoft.com/en-us/library/aa383608%28VS.85%29.aspx或 API:http: //msdn.microsoft.com/en-us/library/aa383608%28VS.85%29.aspx

It's possible to do this on Linux.在 Linux 上可以做到这一点。 You'll find that it is generally not possible to delete a running executable on Windows.您会发现通常无法删除 Windows 上正在运行的可执行文件。 However, you can have Windows delete the EXE for you on the next reboot: http://www.howtodothings.com/computers/a1402-delete-a-running-exe.html但是,您可以让 Windows 在下次重新启动时为您删除 EXE:http: //www.howtodothings.com/computers/a1402-delete-a-running-exe.html

If you want the file deleted after it's been run, you could simply ask the user to delete it.如果您希望文件在运行后删除,您可以简单地要求用户删除它。 If the reason you want this is as a security measure, then what you're doing is misguided.如果您想要这样做的原因是作为一种安全措施,那么您正在做的事情是错误的。 The user could circumvent this by simply making a copy of the file first.用户可以通过简单地首先制作文件的副本来规避这一点。

您可以运行另一个应用程序,该应用程序将等待父进程终止,然后删除其可执行文件。

While it's not possible to self delete a file when it's running it is possible to launch a detached cmd command from the file, then end the file operation before the command executes.虽然无法在文件运行时自行删除文件,但可以从文件中启动分离的 cmd 命令,然后在命令执行之前结束文件操作。 So, if you want to delete a bat file you can just add at the end of the file the line: start cmd /c del %0 and the file would self destruct.因此,如果要删除 bat 文件,只需在文件末尾添加以下行: start cmd /c del %0文件将自毁。

The start cmd will start a new cmd window (detached from your main process). start cmd将启动一个新的 cmd 窗口(与主进程分离)。 The /c tells the windows to execute whatever comes after the /c in the line. /c告诉窗口执行行中/c之后的任何内容。 Then the del will delete the file at the path it is given.然后del将删除给定路径的文件。 The parameter $0 refers to the first command line argument which is usually the name and path to the file that was executed, which is what we want.参数$0指的是第一个命令行参数,它通常是执行文件的名称和路径,这是我们想要的。 (the $0 parameter is the path to the file, you want to pass that to the del command). $0参数是文件的路径,你想将它传递给 del 命令)。

Until that exe is in memory, it will not be able to delete itself.在该 exe 在内存中之前,它将无法自行删除。 However, it can register with the system a task for deleting itself after a set time period of gap when it would be expected to be completing its execution.但是,它可以向系统注册一个任务,以便在预期完成其执行的设定时间间隔后删除自己。

I solved this problem (using Visual Basic) by creating a batchfile that is executed while the process is still running, waits 1sec so the program can close itself and than deletes the program.我通过创建一个批处理文件解决了这个问题(使用 Visual Basic),该批处理文件在进程仍在运行时执行,等待 1 秒,以便程序可以自行关闭,然后删除程序。

You might need to modify it for this will delete every thing in the same folder.您可能需要对其进行修改,因为这将删除同一文件夹中的所有内容。 After your task just call del() and it should work.完成任务后,只需调用del()即可。

 Sub del()
    Dim file As System.IO.StreamWriter
    file = My.Computer.FileSystem.OpenTextFileWriter("del.bat", True)
    file.WriteLine("")
    file.WriteLine("timeout 1")
    file.WriteLine("echo Y | del *.*")
    file.Close()
    Process.Start("del.bat")
    Me.Close()
End Sub

I couldn't find any solutions anywhere else so I'll post my fix here.我在其他任何地方都找不到任何解决方案,所以我将在这里发布我的修复。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
    char* process_name = argv[0];
    char command[256] = "start /min cmd /c del ";
    strcat(command, process_name);

    printf("Attempting to delete self...\n");

    system(command);
    return 0;
}

Normally, trying to use system to call the command prompt to delete an executable would not work because the command prompt that is spawned is a child process that system executes and waits for a return status.通常,尝试使用system调用命令提示符来删除可执行文件是行不通的,因为生成的命令提示符是system执行并等待返回状态的子进程。

This method calls the system to start a command prompt process on its own thread.此方法调用系统在其自己的线程上启动命令提示符进程。

The /min argument starts the process as "hidden". /min参数以“隐藏”方式启动进程。
The /c argument supplies arguments to the spawned command prompt. /c参数为生成的命令提示符提供参数。

I know this is an old thread, but I hopes those that come here in the future.我知道这是一个老话题,但我希望将来能来这里的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅使用其全名来查找安装Windows程序的位置? (以及他的可执行文件的名称) - How can I find where a windows program is installed using only its full name? (And the name of his executable) Windows 10 UWP(桌面桥)应用程序可以重新启动其自己的可执行文件吗? - Can a Windows 10 UWP (Desktop Bridge) app re-launch its own executable? 如何在其自己的环境中从另一个批处理程序运行批处理程序? - How would I run a batch program from another batch program within its own environment? 应用程序在Delphi中关闭后如何启动自己的服务? - How an application can start its own service after it closes in Delphi? Perl 脚本如何知道自己的 memory 占用空间? - How can a Perl script know its own memory footprint? 有没有办法检查外部数据是否通过管道传输到程序或程序是否独立运行? - is there a way to check if external data is piped to a program or if program runs on its own? 可执行错误:无法启动程序 - Executable error : Can't start program 如何在里面创建一个带可执行文件的可执行文件? - How can I create an executable with an executable inside? Windows程序如何暂时更改其时区? - How can a Windows program temporarily change its time zone? 如何使用与其文件扩展名相关联的程序打开文件? - How can you open a file with the program associated with its file extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM