2

How can the currently executing C# application uninstall itself?

I just wanted to do it to be user friendly. It's only a small program, if they install it and don't like it, I don't want to force them to go through the trouble of add/remove, just a quick button click. It's got an (are you sure) dialogue to stop accidents, but beyond that, I thought it would be nice.

sorry about not specifying the install software, I'm using Visual Studio 2008 and its accompanying Publish feature which I've checked to be Windows Installer 3.1.

Process.Start is giving me compilation errors, and isn't being recognized by VS. tried system. as well but am using system so it shouldntv mattered anyway

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Nona Urbiz
  • 4,873
  • 16
  • 57
  • 84

3 Answers3

4

Since you said uninstall, I have to assume that means you used an installer to install your program in the first place.

Also assuming you used Windows Installer (MSI) since you didn't otherwise specify. If so it's simple:

1) In your button click handler, run this command with Process.Start: msiexec.exe /x [your product code]

http://msdn.microsoft.com/en-us/library/aa367988(VS.85).aspx

2) Exit the program immediately (Environment.Exit or any other mechanism)

That said, I'd have to question why you need to do this as it's a pretty unusual behavior.

Adam Sills
  • 16,896
  • 6
  • 51
  • 56
  • 1
    I need to do this. I have a very important update, that greatly improves functionality, speed and eas-of-use AND also fixes a fatal error in the code that can cause several system files to become corrupt, but the customer refuses to stop using the bad version. So, I'm going to force them to. Not understanding why you need to uninstall something is a pretty stupid reason to continue using it under the circumstances. –  Mar 05 '11 at 21:12
  • 1
    How would I find my 'product code'? Are you referring to a GUID? –  Mar 05 '11 at 21:17
  • If it's your installer, you should know what it is because it's part of your project properties. If it's not your installer, use orca (google it "windows installer orca") to find out. – Adam Sills Mar 08 '16 at 16:12
  • it's my own installer, it's a clickonce application, but where is it? Is it in AssemblyInfo.cs? In there I see: `// If the project will be exposed to COM, the following GUID will be used as ID of the types library [assembly: Guid("44993dbc-2e7e-1244-b7e3-da66c80a1121")]` – Redoman Mar 08 '16 at 21:18
  • You can't use msiexec on ClickOnce applications. – Adam Sills Mar 10 '16 at 16:04
1

Well, firstly, how does it install itself?

If you question is how to make a C# application delete itself, see here, among others.

Community
  • 1
  • 1
Noon Silk
  • 54,084
  • 6
  • 88
  • 105
0

It's not a bad idea, but it's pretty non-standard; even ClickOnce application's can't self-uninstall, the Add/Remove Programs tool is really where it belongs.

STW
  • 44,917
  • 17
  • 105
  • 161