[C#] - Self-deletable application + Temp generator

Discussion in 'Mixed Languages' started by Josh Cell, Aug 10, 2012.

  1. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    #1 Josh Cell, Aug 10, 2012
    Last edited by a moderator: Apr 20, 2017
    When you needs of delete the directory of an EXE Application on .NET, it isn't possible when the app is running on the memory. Is very common when you've developing an Uninstaller project that clean himself for an complete uninstall.

    We have this solution for it:

    ---001: Create a temp directory of your application and copy your main EXE into it replacing before InitializeComponent method:

    Code:
                if (!Application.ExecutablePath.ToUpper().Contains(Path.GetTempPath().ToUpper()))
                {
                    string tmpPthName = Path.GetTempPath() + "\\" + Path.GetRandomFileName();
                    string tmpFilName = tmpPthName + "\\" + Path.GetFileName(Application.ExecutablePath);
                    try
                    {
                        Directory.CreateDirectory(tmpPthName);
                        File.Copy(Application.ExecutablePath, tmpFilName);
                        Process.Start(tmpFilName);
                        Application.ExitThread();
                        Environment.Exit(0);
                    }
                    catch { }
                }
                else Directory.SetCurrentDirectory(Path.GetTempPath());
    ---002: Add to FormClosed event this arguments to self-delete the temp EXE when it's running on temp directory:

    Code:
                if (Application.ExecutablePath.ToUpper().Contains(Path.GetTempPath().ToUpper()))
                {
                    int PID = Process.GetCurrentProcess().Id;
                    string cAppPath = @"""" + Path.GetDirectoryName(Application.ExecutablePath)  + @"""";
                    Process p = new Process();
                    p.StartInfo.FileName = "cmd.exe";
                    p.StartInfo.Arguments = "/C TASKKILL /F /PID " + PID + @" & RD /S /Q " + cAppPath;
                    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    p.Start();
                }
    And done :D

    The project always will run in a temp directory leaving the open instance of the main EXE on your hard disk and auto-cleaning on the temp folder after exit.

    Now you can delete the directory or the main EXE of the application without any problem in any time with the code. :schmorch:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,858
    2,112
    60
    Looks like this would integrate well as part of an autoupdate feature.

    Thanks Josh.
     
  3. Carpenter ABCD

    Carpenter ABCD MDL Novice

    Jan 21, 2013
    4
    0
    0
    generating barcode in C# is more difficult in VB for me, however, this is nice info...
     
  4. Pr3acher

    Pr3acher MDL Member

    Aug 24, 2012
    143
    48
    10
    Working on it for C++ and the API.
    Anyaway, thanks