The visual-studio-2013 does not have a Setup generator, as it had the visual-studio-2010 . That's why I've used the Installer Projects extension.
After installing the extension, I added to my project a Setup Project
project located at Other Project Types > Visual Studio Installer
(option created by extension ).
I added a new class to my project called InstallerActions.cs
:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
namespace GiTp
{
[RunInstaller(true)]
public partial class InstallerActions : System.Configuration.Install.Installer
{
public override void Install(IDictionary savedState)
{
base.Install(savedState);
}
public override void Rollback(IDictionary savedState)
{
base.Rollback(savedState);
}
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
if (this.Context.Parameters["cpath"] == "1")
AddPath(this.Context.Parameters["targ"]);
}
public override void Uninstall(IDictionary savedState)
{
Process application = null;
foreach (var process in Process.GetProcesses())
{
if (!process.ProcessName.ToLower().Contains("gitp")) continue;
application = process;
break;
}
if (application != null && application.Responding)
application.Kill();
RemovePath(this.Context.Parameters["targ"]);
base.Uninstall(savedState);
}
private void AddPath(String path)
{
var originalPath = Environment.GetEnvironmentVariable("PATH").TrimEnd(';');
Environment.SetEnvironmentVariable("PATH", originalPath + ';' + path.TrimEnd('\'), EnvironmentVariableTarget.Machine);
}
private void RemovePath(String path)
{
var originalPath = Environment.GetEnvironmentVariable("PATH").TrimEnd(';');
List<string> paths = new List<string>(originalPath.Split(';'));
foreach (string p in paths)
{
if (String.Compare(p, path, true) == 0)
{
paths.Remove(p);
break;
}
}
Environment.SetEnvironmentVariable("PATH", String.Join(";", paths) );
}
}
}
The AddPath(String path)
method registers the PATH
variable and is called in the Commit
method, which is executed after the program is installed.
The parameters this.Context.Parameters["cpath"]
and this.Context.Parameters["targ"]
are reported in the CustomActionData
of the Custom Event property of the extension, as shown:
Propertyvalue:/targ="[TARGETDIR]\" /cpath=[CHECKPATH]
. Note: The property CHECKPATH
is a checkbox of a custom Setup screen.
The RemovePath(String path)
method removes the registry of the PATH
variable and is called in the Uninstall
method, which will be executed when the program is uninstalled.
Note: The setting of the CustomActionData
property is the same as the Commit property, except that% / sub>
Fonts
Creating an MSI / Setup Package for C # Windows Application Using Visual Studio 2010 Setup Project EN
@CiganoMorrisonMendez Answer