Self deletable application in C# in one executable

2020-01-27 11:43发布

Is it possible to make an application in C# that will be able to delete itself in some condition.

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.

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);

7条回答
干净又极端
2楼-- · 2020-01-27 12:39
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.

查看更多
登录 后发表回答