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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1606140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:50:57  来源:igfitidea点击:

How can a program delete its own executable

windowsfileprocess

提问by ata

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 中做到这一点

回答by Greg Hewgill

One way to do this is to use the MoveFileExfunction with the MOVEFILE_DELAY_UNTIL_REBOOTflag and a NULLdestination. According to the documentation, this:

一种方法是使用带有标志和目的地的MoveFileEx函数。根据文档,这是:MOVEFILE_DELAY_UNTIL_REBOOTNULL

registers the lpExistingFileNamefile to be deleted when the system restarts. If lpExistingFileNamerefers to a directory, the system removes the directory at restart only if the directory is empty.

注册系统重新启动时要删除的lpExistingFileName文件。如果lpExistingFileName指的是一个目录,则只有当目录为空时,系统才会在重新启动时删除该目录。

回答by Samy Bencherif

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

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

回答by uckelman

It's possible to do this on Linux. You'll find that it is generally not possible to delete a running executable on 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

在 Linux 上可以做到这一点。您会发现通常无法删除 Windows 上正在运行的可执行文件。但是,您可以让 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.

如果您想在运行后删除该文件,您可以简单地要求用户将其删除。如果您想要这样做的原因是作为一种安全措施,那么您所做的就是错误的。用户可以通过简单地首先复制文件来规避这一点。

回答by Alex Reitbort

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

回答by Pavel Alexeev

You can run another application, which would wait for parent process to terminate, and then delete its executable.

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

回答by Gur Telem

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. 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 %0and the file would self destruct.

虽然在文件运行时无法自行删除文件,但可以从文件中启动一个分离的 cmd 命令,然后在命令执行之前结束文件操作。所以,如果你想删除一个 bat 文件,你可以在文件的末尾添加一行: start cmd /c del %0并且文件会自毁。

The start cmdwill start a new cmd window (detached from your main process). The /ctells the windows to execute whatever comes after the /cin the line. Then the delwill delete the file at the path it is given. The parameter $0refers to the first command line argument which is usually the name and path to the file that was executed, which is what we want. (the $0parameter is the path to the file, you want to pass that to the del command).

start cmd会启动一个新cmd窗口(从主进程分离)。该/c通知窗口执行后无论发生什么事/c在该行。然后del将删除给定路径中的文件。参数$0指的是第一个命令行参数,它通常是执行的文件的名称和路径,这正是我们想要的。($0参数是文件的路径,您希望将其传递给 del 命令)。

回答by SomeCoffeePlz

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

回答by techzen

Until that exe is in memory, it will not be able to delete itself. 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.

在该 exe 进入内存之前,它将无法删除自己。但是,它可以向系统注册一个任务,以便在预期完成其执行的设定时间间隔后删除自身。