El TecnoBaúl de Kiquenet

Kiquenet boring stories

Archive for the ‘SetupProjects’ Category

Modificar programáticamente ProductCode de Setup Project

Posted by kiquenet en 17 agosto 2012

Existen varias alternativas:

Proyecto en VS .NET C#:

VS.NET Deployment Project Updater


En C# con expresiones regulares:


static void Main(string[] args)
string setupFileName = @"<Replace the path to vdproj file>";
StreamReader reader = File.OpenText(setupFileName);
string file = string.Empty;

Regex expression = new Regex(@”(?:\”"ProductCode\”" =
Regex expression1 = new Regex(@”(?:\”"UpgradeCode\”" =
file = reader.ReadToEnd();

file = expression.Replace(file, “\”ProductCode\” = \”8:{” +
Guid.NewGuid().ToString().ToUpper() + “}”);
file = expression1.Replace(file, “\”UpgradeCode\” = \”8:{“
+ Guid.NewGuid().ToString().ToUpper() + “}”);
// Close the file otherwise the compile may not work

TextWriter tw = new StreamWriter(setupFileName);
// close the stream

Programmatically change the ‘ProductName’, ‘ProductCode’, ‘UpgradeCode’ and ‘ShortCut’ depending on build Configuration.

  1. Select the “Setup Project” in vs2005 and press F4.
  2. Change the “PostBuildEvent” towscript “$(ProjectDir)UpdateMSI.vbs” “$(BuiltOuputPath)” “$(ProjectDir)” $(Configuration).
  3. Place the UpdateMSI.vbs file in “$(ProjectDir)”.
BuiltOuputPath = Wscript.Arguments(0)

ProjectDir = Wscript.Arguments(1)

Configuration = Wscript.Arguments(2)

ProductName = "MyApplication - " &amp; Configuration

ShortcutName = "MYAPP~2|" &amp; ProductName 'Check this with orca.exe

select case Configuration

case "Debug"

UpgradeCode = "{11A3CB76-5DAC-4B9F-A993-401BE1D95A9E}"

ProductCode = "{E1BFC46C-6226-459C-BC4F-8DE17888ED62}"

case "Test"

UpgradeCode = "{B15A5EAA-FA9C-4BB7-AD68-29DAE5C95846}"

ProductCode = "{F183C852-5DBC-498C-89F1-2D6CDFF515B6}"

case "Production"

UpgradeCode = "{E14EC8A9-FA0B-47B8-84EB-4EC7ED278E7E}"

ProductCode = "{016C6EF8-A23F-4822-96B2-E0A1D74641EE}"

case Else

end select

'guid = CreateObject("Scriptlet.TypeLib").Guid

'UpgradeCode = left(guid, len(guid) - 2)

Const msiOpenDatabaseModeTransact = 1

Dim propertyKey, propertyValue

Dim installer, database, view, record

Set installer = CreateObject("WindowsInstaller.Installer")

Set db = Installer.OpenDatabase(BuiltOuputPath, msiOpenDatabaseModeTransact)

Set view = db.OpenView("UPDATE Property SET Value = '" &amp; ProductName &amp; "' WHERE Property = '" &amp; "ProductName" &amp; "'")


Set view = db.OpenView("UPDATE Property SET Value = '" &amp; ProductCode &amp; "' WHERE Property = '" &amp; "ProductCode" &amp; "'")


Set view = db.OpenView("UPDATE Property SET Value = '" &amp; UpgradeCode &amp; "' WHERE Property = '" &amp; "UpgradeCode" &amp; "'")


Set view = db.OpenView("UPDATE Shortcut SET Name = '" &amp; ShortcutName &amp; "' WHERE Description = 'MyApplication Application'")


'Set view = db.OpenView("UPDATE Upgrade SET UpgradeCode = '" &amp; UpgradeCode &amp; "'")



Set installer = Nothing

Set db = Nothing

Set view = Nothing

Con MSBuild:

How to auto-increment assembly version using a custom MSBuild task


También hay herramientas de terceros como Stampver:




Posted in .NET, SetupProjects | Leave a Comment »

Setup Projects y scripts “uninstall”

Posted by kiquenet en 17 agosto 2012

If the user wants to uninstall the product, he can go to "Add/Remove Programs", or he can just double-click the uninstall.cmd. The contents are:

%windir%\system32\msiexec /x {CC3EB7BF-DD82-48B9-8EC5-1B0B62B6D285}

The GUID there is the ProductCode from the Setup Project in Visual Studio.


How can I dynamically generate a batch file that contains the ProductCode for the, at build time?

"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)

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

2) 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.

3) 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].

4) 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.


Otra opciones más enrevesadas:

1.) make your application read command line arguments, the code below is from a WPF project

public partial class App : Application


  void Application_Startup(object sender,StartupEventArgs e)


    for(int i = 0;i != e.Args.Length;++i)


      if(e.Args[i].Split(‘=’)[0].ToLower() == "/u")


        string guid = e.Args[i].Split(‘=’)[1];

        string path = Environment.GetFolderPath(Environment.SpecialFolder.System);

        ProcessStartInfo uninstallProcess = new ProcessStartInfo(path+"\\msiexec.exe","/x "+guid);







To add the uninstall link in the deployment project you just need to follow the steps below:

  1. Select your deployment project and go to the file system editor.
  2. Using the file system editor you navigate to the desired location of the install file.
  3. From the decired location you create a new shortcut to your primary output and name it Uninstall – or whatever else you would like.
  4. In the properties of the new shortcut you set the arguments property to /u=[ProductCode]

2.) creates an uninstall.cmd. It runs as a custom action during installation.

var fso, ts;
var ForWriting= 2;
fso = new ActiveXObject("Scripting.FileSystemObject");

var parameters = Session.Property("CustomActionData").split("|"); 
var targetDir = parameters[0];
var productCode = parameters[1];

ts = fso.OpenTextFile(targetDir + "uninstall.cmd", ForWriting, true);

ts.WriteLine("@echo off");
ts.WriteLine("goto START");
ts.WriteLine(" Uninstall.cmd");
ts.WriteLine("@REM The uuid is the 'ProductCode' in the Visual Studio setup project");
ts.WriteLine("%windir%\\system32\\msiexec /x " + productCode);

Una buena referencia para Extending VS Setup Projects:


Gran referencia que explica en detalle los Setup Projects: PackageCode, ProductCode, UpgradeCode, herramienta Orca, etc.






Posted in .NET, SetupProjects, VisualStudio | Leave a Comment »