Results 1 to 4 of 4

Thread: Destroy Self

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Destroy Self

    Destroy Self is a small snippet of code to allow you to delete your own exe
    Only real uses I can see this for is something like an Uninstaller, But
    I am sure you guys will have other ideas, anyway hope you find it useful
    Comments and suggestions welcome.

    Code:
            private void DestroySelf()
            {
                Process p = new Process();
                string Bat_File = "delus.bat";
                string Bat_Path = Path.Combine(Directory.GetCurrentDirectory(), Bat_File);
                string ExeName = new FileInfo(Application.ExecutablePath).Name;
                StreamWriter sw = new StreamWriter(Bat_Path);
    
                //Create batch file to delete main exe
                sw.WriteLine("attrib \"" + ExeName + "\"" + " -a -s -r -h");
                sw.WriteLine(":Repeat");
                sw.WriteLine("del " + "\"" + ExeName + "\"");
                sw.WriteLine("if exist \"" + ExeName + "\"" + " goto Repeat");
                sw.WriteLine("del \"" + Bat_File + "\"");
                sw.Close();
    
                //Start process to execute batch file.
                p.StartInfo.FileName = "delus.bat";
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.UseShellExecute = false;
    
                try
                {
                    //Attempt to start process.
                    p.Start();
                }
                catch (Exception)
                {
                    //Something went wrong :( close error found.
                    Close();
                }
            }
    Example

    Code:
            private void cmdClose_Click(object sender, EventArgs e)
            {
                //Example.
                DestroySelf();
                Close();
            }

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Destroy Self

    The following 2 articles describes a number of alternative techniques:

    Self deleting executables

    Self-deleting Executables
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Destroy Self

    string Bat_File = "delus.bat";
    Hi ,ben i would like to ask what is the Role of delus.bat File .and what is the contains of this bat file .

  4. #4

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: Destroy Self

    Quote Originally Posted by firoz.raj View Post
    Hi ,ben i would like to ask what is the Role of delus.bat File .and what is the contains of this bat file .
    it just creates the batch file after your app has ended, it checks if your app is found if so it will loop until it deleted, the batch file then deletes it self.
    Here the code from the batch file.

    Code:
    attrib somexe.exe -a -s -r -h
    :repeat
    del someexe.exe
    if exsits someexe.exe goto repeat
    del batchfile.bat

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width