How to add my program to PATH in Windows.NET?

After installing my .NET program, how do I configure the PATH system to include my absolute program directory so that the user can run my .exe from any directory in the console?

Note. I want this to be done automatically if the end user does not have to manually add the PATH itself.

+3
source share
3 answers

I assume that you are using the VS2008 built-in installer, not InstallShield or Wise or something like that (both of them have much better ways).

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

, , TARGETDIR Path...

/VariableName = "" /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);
            }
        }
    }
}

System.Configuration.Install .

+4

:

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

, , .

, , ...

+3

. .

, setx.exe( IIRC), , , , :)

Or, my favorite, 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_
+2
source

Source: https://habr.com/ru/post/1709419/


All Articles