一个可执行文件中的 C# 中的自删除应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1305428/
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
Self deletable application in C# in one executable
提问by George
Is it possible to make an application in C# that will be able to delete itself in some condition.
是否可以在 C# 中创建一个应用程序,该应用程序将能够在某些情况下删除自身。
I need to write an updater for my application but I don't want the executable to be left after the update process.
我需要为我的应用程序编写一个更新程序,但我不希望在更新过程后留下可执行文件。
There is an official .Net OneClick but due to some incompatibilities with my HTTP server and some problems of OneClick itself I'm forced to make one myself.
有一个官方的 .Net OneClick,但由于与我的 HTTP 服务器的一些不兼容以及 OneClick 本身的一些问题,我不得不自己制作一个。
George.
乔治。
[EDIT] In more details:
[编辑]更多细节:
I have: Application Executable which downloads the updater ("patch", but not exactly) this "patch" updates the application executable itself.
我有:应用程序可执行文件下载更新程序(“补丁”,但不完全是)这个“补丁”更新应用程序可执行文件本身。
Application executes as folowed:
应用程序执行如下:
Application: Start -> Check Version -> Download new Updater -> Start Updater -> exit;
Updater: Start -> do it's work -> start Application Executable -> self delete (this is where I get stuck);
采纳答案by James
If you use Process.Start you can pass in the Del
parameter and the path to the application you wish to delete.
如果您使用 Process.Start,您可以传入Del
参数和要删除的应用程序的路径。
ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments="/C choice /C Y /N /D Y /T 3 & Del "+
Application.ExecutablePath;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName="cmd.exe";
Process.Start(Info);
Code snippet taken from this article
摘自本文的代码片段
回答by chrischu
Mhh so let me get this straight. You got some application.exe and your updater application updater.exe?
嗯,让我直说吧。你有一些 application.exe 和你的更新程序 updater.exe 吗?
So when you start your application.exe it checks some webserver for a newer version and then starts updater.exe. And you want updater.exe to delete itself after it has finished updating? Or do you want to delete the downloaded patch (or similar)? Please be a bit more precise.
因此,当您启动 application.exe 时,它会检查某些网络服务器是否有更新版本,然后启动 updater.exe。您希望 updater.exe 在完成更新后自行删除吗?还是要删除下载的补丁(或类似补丁)?请准确一点。
Consider that when you are deleting updater.exe you must recreate it for the next update process.
考虑到当您删除 updater.exe 时,您必须为下一次更新过程重新创建它。
回答by James
your second line can be
你的第二行可以是
Updater: Star -> do it's work -> start Application Executable -> Updater Exits -> Application deletes your Updater.exe
回答by Eamon Nerbonne
Couldn't you simply delete the updater from within the application? i.e.:
您不能简单地从应用程序中删除更新程序吗?IE:
Application: Start -> [Delete old updater if present] -> Check version -> Download new updater -> Start updater -> exit;
Updater: Start -> Perform update -> Start application -> exit;
Application: Start -> [Delete old updater if present] -> ...
应用程序:开始 -> [如果存在,则删除旧更新程序] -> 检查版本 -> 下载新更新程序 -> 启动更新程序 -> 退出;
更新程序:开始 -> 执行更新 -> 启动应用程序 -> 退出;
应用程序:开始 -> [如果存在,则删除旧更新程序] -> ...
回答by Mark Brackett
It's tricky without introducing yet another process (that you'd then want to delete as well, no doubt). In your case, you already have 2 processes - updater.exe and application.exe. I'd probably just have the Application delete updater.exe when it's spawned from there - you could use a simple command line arg, or an IPC call from updater.exe to application.exe to trigger it. That's not exactly a self deleting EXE, but fulfills the requirements I think.
如果不引入另一个过程(毫无疑问,您随后也想删除),这会很棘手。在您的情况下,您已经有 2 个进程 - updater.exe 和 application.exe。我可能只是在从那里生成应用程序时删除 updater.exe - 您可以使用简单的命令行参数,或从 updater.exe 到 application.exe 的 IPC 调用来触发它。这不完全是一个自我删除的 EXE,但满足我认为的要求。
For the full treatment, and other options you should read the definitive treatment of self deleting EXEs. Code samples are in C (or ASM), but should be p/invokable.
对于完整的处理和其他选项,您应该阅读自我删除 EXE 的最终处理。代码示例在 C(或 ASM)中,但应该是 p/invokable。
I'd probably try CreateFilewith FILE_FLAG_DELETE_ON_CLOSE
for updater.exe with something like (psuedo code):
我可能会尝试使用CreateFilewith FILE_FLAG_DELETE_ON_CLOSE
for updater.exe 类似(伪代码):
var h = CreateFile(
"updater.exe",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_DELETE,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE
);
byte[] updaterBytes = GetUpdaterBytesFromWeb();
File.WriteAllBytes("updater.exe", updaterBytes);
Process.Start("updater.exe");
Once application.exe exits, updater.exe has a file handle of 1. When updater.exe exits, it drops to 0 and should be deleted.
一旦 application.exe 退出,updater.exe 的文件句柄为 1。当 updater.exe 退出时,它会降为 0,应该删除。
回答by Emre Aydinceren
I suggest you use a batch file as a bootstrap and have it delete itself and the exe afterwards
我建议您使用批处理文件作为引导程序,然后删除自身和 exe
public static class Updater
{
public static void Main()
{
string path = @"updater.bat";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("updater.exe");
sw.WriteLine("delete updater.exe /y");
sw.WriteLine("delete updater.bat /y");
}
System.Process.Start(path);
}
else
{
RunUpdateProcess();
}
}
private void RunUpdateProcess()
{
.....
}
}
回答by reza63
public void uninstall() {
string app_name = Application.StartupPath + "\" + Application.ProductName + ".exe";
string bat_name = app_name + ".bat";
string bat = "@echo off\n"
+ ":loop\n"
+ "del \"" + app_name + "\"\n"
+ "if Exist \"" + app_name + "\" GOTO loop\n"
+ "del %0";
StreamWriter file = new StreamWriter(bat_name);
file.Write(bat);
file.Close();
Process bat_call = new Process();
bat_call.StartInfo.FileName = bat_name;
bat_call.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
bat_call.StartInfo.UseShellExecute = true;
bat_call.Start();
Application.Exit();
}
self delete by an external executable file ".bat" for windows form applications.
对于 Windows 窗体应用程序,通过外部可执行文件“.bat”自我删除。