How to add Directory to the Windows Path variable during installation?

6

I developed a Console application in C # with Visual Studio 2013 and wanted the installer to add the solution directory to the Windows Path variable during installation. It is possible? What is the procedure to perform this change in the system?

Note: I'm not using third-party installers, just Visual Studio 2013. I noticed that 2010 already has a Setup generator, however 2013 only has Publish.

    
asked by anonymous 22.05.2015 / 21:24

3 answers

1

The does not have a Setup generator, as it had the . 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
  • 26.05.2015 / 19:09
    6

    For the installer made via "Inno Setup" , it could be done this way adding the section [Registry].

      

    Setting Environment Variables

         

    Environment variables are stored as string values in the registry, so it is possible to manipulate them using the [Registry] section. System-wide environment variables are located at:   HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment   User-specific environment variables are located at:   HKEY_CURRENT_USER \ Environment

    Reference: link

        
    22.05.2015 / 21:38
    5

    You can write an application that does this in C #:

    Using Environment.SetEnvironmentVariable :

    var originalPath = Environment.GetEnvironmentVariable("PATH");
    Environment.SetEnvironmentVariable("PATH", "C:\Minha\Aplicacacao\Console.exe;" + originalPath);
    

    Only works if the application runs in elevated mode.

    If you are going to generate a .msi using WiX Toolset , see Environment part.

    EDIT

    Apparently PATH is a special case of environment variable. It is recommended to change it through modifications to the Windows registry:

    string chave = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";         
    string pathAntigo = (string)Registry.LocalMachine.CreateSubKey(chave).GetValue("Path", "", RegistryValueOptions.DoNotExpandEnvironmentNames);
    
    Registry.LocalMachine.CreateSubKey(chave).SetValue("Path", pathAntigo + ";C:\Minha\Aplicacacao\Console.exe;", RegistryValueKind.ExpandString);
    
        
    22.05.2015 / 21:39