# VBForums CodeBank > CodeBank - C# >  Destroy Self

## BenJones

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.



```
        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*



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

----------


## Bonnie West

The following 2 articles describes a number of alternative techniques:

Self deleting executables

Self-deleting Executables

----------


## firoz.raj

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

----------


## BenJones

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



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

----------

