简体   繁体   中英

Programmatically Getting Visual Studio Setup Project's ProductCode for Application

I have an installer created using Visual Studio's Setup and Deployment that typically does pretty good.

From time to time, one of my releases is flawed and I need a way to quickly reverse my decision to go with my fabulous upgrade.

I can create a link by including a custom class that inherits from System.Configuration.Installer or create an option in my application to uninstall itself so that I can re-install the old version using something like this:

//using System.Diagnostics;
public static void Uninstall(string productCode, string option) {
  if (!String.IsNullOrEmpty(productCode) && !String.IsNullOrEmpty(option)) {
    string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
    string param1 = string.Format("{0} {1}", path, @"\msiexec.exe");
    string param2 = string.Format("{0} {1}", option, productCode);
    ProcessStartInfo psInfo = new ProcessStartInfo(param1, param2);
    // NOTE: `option` = /i to repair or remove; /x to remove only
    try {
      Process.Start(psInfo);
      return;
    } catch (Exception) { } // the uninstall failed
  }
}

So...

My question: How do I get the ProductCode for my installation?

NOTE: I understand Microsoft recommends going through the Add/Remove Programs (XP) or Programs and Features (Vista/7), but my application is going in a manufacturing environment where most PCs have these features removed (using tweak or some various tool) by our Network Administrators.

MSI安装失败,因为“已安装此产品的另一个版本”有一个VBS片段,您可能会发现它可用作模型。

An answer by Ben in the SO article In a Visual Studio setup project, How do I generate an uninstall script? got me what I needed.

Summarized here:

"Using Visual Studio 2005/2008, you don't need to write any code to add a uninstall option for a Setup project (Yes I know some people can write code to do it)

  • In the Setup Project –> File System windows –> Right Click “File System on Target machine” –> add a Special Folder, select System Folder;

  • Into this system folder Add a file. Browse for msiexec.exe from local System32 folder and add it. Override default properties of this file as follows:

Condition:=Not Installed (make sure you put 'Not Installed' exactly like that, same case and everything), Permanent:=True, System:=True, Transitive:=True, Vital:=False.

  • Create a new shortcut under the 'Users Program Menu', Set Target to the System Folder which you created in the step 1. and point it's at the msiexec.exe. Rename the shortcut to 'Uninstall Your Application'. Set the Arguments property to /x{space}[ProductCode].

  • Build the project, ignore warning about the fact that msiexec should be excluded, DONT exclude it or the setup project wont build.

The 'Not Installed' condition and Permananet:=True ensure that the msiexec.exe is only placed into the system folder as part of the install IF it doesn't aready exist, and it is not removed on an uninstall - therefore it;s pretty safe to ignore that warning and just go for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM