The visual-studio-2013 does not have a generator Setup (Only the Publish), as had the visual-studio-2010. That’s why I used the extension Installer Projects.
After installing the extension, I added a project to my project Setup Project
located in 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 method AddPath(String path)
performs the recording of the variable PATH
and is called in the method Commit
, which is executed after the installation of the program.
The parameters this.Context.Parameters["cpath"]
and this.Context.Parameters["targ"]
are informed on the property CustomActionData
of Custom Event of the extension, as illustrated:
Property value: /targ="[TARGETDIR]\" /cpath=[CHECKPATH]
.
Obs.: The estate CHECKPATH
is a checkbox of a customized screen Setup.
The method RemovePath(String path)
removes the record of the variable PATH
and is called in the method Uninstall
, which will run on program uninstallation.
Obs.: The setting of the property CustomActionData
is the same as Commit, except that it is not necessary to send the property CHECKPATH
.
Sources
- Creating an MSI/Setup Package for C# Windows Application Using a Visual Studio 2010 Setup Project EN
- Reply from @Ciganomorrisonmendez
What use would that be?
– Andrew Alex
My app is like Console.
– KaduAmaral
The installer is that of VS itself or of third parties?
– rubStackOverflow
In the case of the VS. I’m actually studying C# now, and it’s my first program with VS, I’m not sure if I’m proceeding in the right way, but at first I’m only using VS2013.
– KaduAmaral