How can I make my .NET application erase itself?

前端 未结 11 715
梦如初夏
梦如初夏 2020-12-08 10:37

How can I make my C# app erase itself (self-destruct)? Here\'s two ways that I think might work:

  • Supply another program that deletes the main program. How is t
11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 11:21

    A correction to @Bobby answer, in case people will find it useful - executable path needs to be quoted. Additionally, below is setting cmd.exe window to be hidden (otherwise it flashes as a black console window) and converted to run without relying on System.Windows.Forms assembly (the Application class).

     
    var exepath = Assembly.GetEntryAssembly().Location;              
    var info = new ProcessStartInfo("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del \"" + exepath + "\"");
    info.WindowStyle = ProcessWindowStyle.Hidden;
    Process.Start(info).Dispose();
    Environment.Exit(0);
    

提交回复
热议问题