Uninstall C# windows application from self

2019-07-14 05:43发布

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

3条回答
Animai°情兽
2楼-- · 2019-07-14 05:47

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.

查看更多
在下西门庆
3楼-- · 2019-07-14 05:47

Well, firstly, how does it install itself?

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

查看更多
何必那么认真
4楼-- · 2019-07-14 06:03

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.

查看更多
登录 后发表回答