How can I make my .NET application erase itself?

前端 未结 11 668
梦如初夏
梦如初夏 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条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 11:07

    This is the Uninstall.exe:

    1. Shutdown.

    2. Wait for 3 sec.

    3. Try to kill that task if it is still running.

    4. Wait for 3 sec.

    5. Delete the app directory with the Uninstall.exe in it.

       public void Uninstall()
       {
           var delPath = AppDomain.CurrentDomain.BaseDirectory;
      
           var procId = Process.GetCurrentProcess().Id;
      
           var psi = new ProcessStartInfo
           {
               FileName = "cmd.exe",
               Arguments = $"/C timeout 3 & Taskkill /F /PID {procId} & timeout 3 & rd /s /q \"{delPath}\"",
               CreateNoWindow = true,
               WindowStyle = ProcessWindowStyle.Hidden
           };
           Process.Start(psi);
           Application.Current.Shutdown();
       }
      

提交回复
热议问题