Sunday, December 12, 2010

Making an application delete itself

While developing an uninstall application in C#, I wanted to delete the uninstall.exe after it has uninstalled the application.

Doing some googling and research I came up with this code.

This code is written in C# language.

Process.Start("cmd.exe", "/C choice /C Y /N /D Y /T 2 & Del " + Application.ExecutablePath);
Application.Exit();

The following is a description of what those parameters mean.

  1. /C causes a command window to appear and run the command specified.. it then causes the window to close automatically.
  2. Choice /C Y /N /D Y /T 2 displays an empty, flashing prompt. However, the /T 2 means that the prompt will automatically select the default choice Y (/D Y) after 2 seconds.
  3. & is used to chain multiple commands together on a single line in a batch file.
  4. Del " + ....Everything after the & can be replaced with anything you want to happen after the second second delay.

Finally the code I used is
ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments="/C choice /C Y /N /D Y /T 2 & Del "+ Application.ExecutablePath;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName="cmd.exe";
Process.Start(Info);

No comments:

Post a Comment