Self deletable application in C# in one executable

前端 未结 7 1809
北荒
北荒 2020-11-30 23:26

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

相关标签:
7条回答
  • 2020-11-30 23:32

    Couldn't you simply delete the updater from within the application? i.e.:

    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] -> ...

    0 讨论(0)
  • 2020-11-30 23:33

    I suggest you use a batch file as a bootstrap and have it delete itself and the exe afterwards

    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()
        {
            .....
        }
    }
    
    0 讨论(0)
  • 2020-11-30 23:38

    If you use Process.Start you can pass in the Del parameter and the path to the application you wish to delete.

    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

    0 讨论(0)
  • 2020-11-30 23:39

    Mhh so let me get this straight. You got some application.exe and your updater application 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.

    Consider that when you are deleting updater.exe you must recreate it for the next update process.

    0 讨论(0)
  • 2020-11-30 23:42

    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.

    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.

    I'd probably try CreateFile with FILE_FLAG_DELETE_ON_CLOSE for updater.exe with something like (psuedo code):

     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.

    0 讨论(0)
  • 2020-11-30 23:54
    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.

    0 讨论(0)
提交回复
热议问题