views:

532

answers:

3

For Windows:

After my .NET program is installed, how do I set the system PATH to include my program absolute directory such that the user can launch my .exe from any directory within the console?

Note: I want this to be done automatically without the end-user having to manually add the PATH himself.

+2  A: 

Most installers will allow you to append to the system path environment variable. Check the documentation for this feature.

If you're installing manually, you can use setx.exe (from the resource kit IIRC) to modify the path - but be careful, you do not want to replace the existing path with just your app's directory, he said with experience :)

Or, my favourite, use WMI in a script:

eg.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objVariable = objWMIService.Get("Win32_Environment").SpawnInstance_

objVariable.Name = "Path"
objVariable.UserName = "System"
objVariable.VariableValue = "c:\myapp"
objVariable.Put_
gbjbaanb
I just tried this out in C#..but didnt see any difference...did I miss something? ManagementClass clss = new ManagementClass("Win32_Environment"); clss["UserName"] = "System"; clss["Name"] = "Path"; clss["VariableValue"] = "c:\\myapp"; clss.Put();
ShaChris23
I have no idea, I only do my WMI scripting in .vbs scripts that I run using cscript.exe
gbjbaanb
+3  A: 

You can access and append to the current path at this registry location:

HLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path

This is a change better made in your installer, not in your actual application.

Make sure you append to the registry value, and don't just set it...

Michael Petrotta
If you want the user's PATH, it's in `HKEY_CURRENT_USER\Environment\PATH`
Pat
+4  A: 

I am assuming you're using the VS2008 built-in installer and not InstallShield or Wise or something like that (which both have much better ways).

You can create an installer class that adds it (see below).

You then add your installer class as a custom action for install and uninstall and add custom actions data with the path you want, for example to add the TARGETDIR to the Path ...

/VariableName="Path" /Value="[TARGETDIR]\"

using System;
using System.ComponentModel;

namespace Emv
{
    [RunInstaller(true)]
    public class Installer : System.Configuration.Install.Installer
    {
        public Installer()
        {

        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);

            try
            {
                var varName  = this.Context.Parameters["VariableName"];
                var valToAdd = this.Context.Parameters["Value"];
                var newVal   = String.Empty;

                var curVal = Environment.GetEnvironmentVariable(varName);

                if (curVal != null && curVal.Contains(valToAdd))
                {
                    return;
                }

                newVal = (curVal == String.Empty) ? valToAdd 
                                                      : curVal + ";" + valToAdd;

                Environment.SetEnvironmentVariable(varName, newVal,
                      EnvironmentVariableTarget.Machine);
            }
            catch (Exception ex)
            {
                // message box to show error
                this.Rollback(stateSaver);
            }
        }
    }
}

A reference to System.Configuration.Install is required for this code.

JP Alioto
you are exactly right..i'm using vs2008 built-in installer. perhaps i should just use InstallShield. thanks for the code though..i didnt know that i could create my own custom installer.
ShaChris23