-
Oct 7th, 2013, 03:44 PM
#1
Thread Starter
Fanatic Member
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();
}
-
Oct 9th, 2013, 02:37 AM
#2
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)
-
Oct 11th, 2013, 06:03 AM
#3
Frenzied Member
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 .
-
Oct 11th, 2013, 07:51 AM
#4
Thread Starter
Fanatic Member
Re: Destroy Self
Originally Posted by firoz.raj
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|