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

Asked

Viewed 1,631 times

6

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

Obs.: I am not using third party installers, only Visual Studio 2013. I noticed that 2010 already has a Setup generator, but 2013 only has Publish.

  • What use would that be?

  • My app is like Console.

  • The installer is that of VS itself or of third parties?

  • 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.

3 answers

6

For the via installer "Inno Setup", could be done that way adding to 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: http://www.jrsoftware.org/isfaq.php#env

  • For me to create an installer with the Inno Setup I compile my code and create the setup with compiled files, or it compiles?

  • This you compile your code normally, ai in Inno you add the files you need, Readme, License and things like that. In the installation it is possible to check if there is a framework installed in the machine, and install it if necessary. It is very simple look ai: http://www.linhadecodigo.com.br/artigo/1244/criando-um-pacote-de-instalacao-com-inno-setup.aspx @Kaduamaral

5

You can write an app 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 high mode.

If it will generate a .msi using Wix Toolset, see the Environment part.


EDIT

Apparently PATH is a special case of environment variable. Recommended is to change it through modifications in 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);
  • Is this high mode like when the program runs in administrator mode? This application should be bundled with my solution?

  • @Kaduamaral That’s right. If you’re going to do it by installer, it’s best to use the installer method, which already does everything for you. The idea was to show that it is possible to change environment variables using C# as well.

  • Does the VS installer allow that when the solution installation is finished this "configurator" is executed (and in high mode)? If so, could you add the steps to this in your reply? :)

  • I noticed that in Visual Studio, in New > Project > Other Projects Types > Setup and Deployment has a type "Enable Installshield Limited Edition" would be possible to do with it?

  • I think so: http://helpnet.installshield.com/installshield18helplib/IHelpISXSetEnvVariable.htm

  • Opa Gypsy, I used this way, I even created a log file, it adds everything right, but when I go in the properties of the computer and I go in the environment variables, there is the change, neither in the system nor in the user...

  • @Kaduamaral I don’t know exactly what it can be. I put an alternative solution.

  • I found out, I added the third parameter EnvironmentVariableTarget.Machine and it worked... :)

  • 1

    Gypsy, thank you for your help, but I will answer the question with the steps I have taken until I get to the part you helped me because it is incomplete answer, considering the scope "during installation" of the question.

  • Gypsy can give me a hand in that question?

Show 5 more comments

1


The does not have a generator Setup (Only the Publish), as had the . 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:

Custom Action Commit

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

  1. Creating an MSI/Setup Package for C# Windows Application Using a Visual Studio 2010 Setup Project EN
  2. Reply from @Ciganomorrisonmendez

Browser other questions tagged

You are not signed in. Login or sign up in order to post.