如何在Windows .NET中将程序添加到PATH?

时间:2022-07-02 02:40:02

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?

安装我的.NET程序后,如何设置系统PATH以包含我的程序绝对目录,以便用户可以从控制台内的任何目录启动我的.exe?

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

注意:我希望自动完成此操作,而最终用户不必自己手动添加PATH。

3 个解决方案

#1


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

我假设您正在使用VS2008内置安装程序,而不是InstallShield或Wise或类似的东西(两者都有更好的方法)。

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

然后,将安装程序类添加为安装和卸载的自定义操作,并使用所需的路径添加自定义操作数据,例如将TARGETDIR添加到路径...

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

此代码需要对System.Configuration.Install的引用。

#2


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

确保附加到注册表值,而不是只设置它...

#3


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 :)

如果你手动安装,可以使用setx.exe(来自资源工具包IIRC)修改路径 - 但要小心,你不想只用你的应用程序目录替换现有的路径,他说有经验:)

Or, my favourite, use WMI in a script:

或者,我最喜欢的,在脚本中使用WMI:

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_

#1


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

我假设您正在使用VS2008内置安装程序,而不是InstallShield或Wise或类似的东西(两者都有更好的方法)。

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

然后,将安装程序类添加为安装和卸载的自定义操作,并使用所需的路径添加自定义操作数据,例如将TARGETDIR添加到路径...

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

此代码需要对System.Configuration.Install的引用。

#2


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

确保附加到注册表值,而不是只设置它...

#3


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 :)

如果你手动安装,可以使用setx.exe(来自资源工具包IIRC)修改路径 - 但要小心,你不想只用你的应用程序目录替换现有的路径,他说有经验:)

Or, my favourite, use WMI in a script:

或者,我最喜欢的,在脚本中使用WMI:

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_