Geo*_*rge 29 .net c# self-updating self-destruction
是否可以在C#中创建一个能够在某些条件下删除自己的应用程序.
我需要为我的应用程序编写更新程序,但我不希望在更新过程后留下可执行文件.
有一个官方的.Net OneClick但由于与我的HTTP服务器有些不兼容以及OneClick本身的一些问题,我不得不自己制作一个.
乔治.
[编辑]更多细节:
我有:Application Executable下载更新程序("补丁",但不完全)这个"补丁"更新应用程序可执行文件本身.
应用程序执行如下:
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);
Run Code Online (Sandbox Code Playgroud)
Jam*_*mes 34
如果使用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);
Run Code Online (Sandbox Code Playgroud)
从本文中获取的代码段
小智 9
我建议您使用批处理文件作为引导程序,然后删除自己和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()
{
.....
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有引入另一个进程(你当然也希望删除,毫无疑问),这很棘手.在您的情况下,您已经有2个进程 - updater.exe和application.exe.我可能只是从那里生成应用程序删除updater.exe - 您可以使用简单的命令行arg,或从updater.exe到application.exe的IPC调用来触发它.这不是一个自我删除EXE,但满足我认为的要求.
对于完整治疗和其他选项,您应该阅读自我删除EXE的最终处理.代码示例使用C(或ASM),但应该是p/invokable.
我可能尝试使用updater.exe的CreateFile与FILE_FLAG_DELETE_ON_CLOSE类似(伪造的代码):
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");
Run Code Online (Sandbox Code Playgroud)
一旦application.exe退出,updater.exe的文件句柄为1.当updater.exe退出时,它将降为0并应删除.
| 归档时间: |
|
| 查看次数: |
34421 次 |
| 最近记录: |