且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中设置系统环境变量?

更新时间:2022-05-24 01:55:39

的的文档告诉你如何做到这一点。

The documentation tells you how to do this.

调用 SetEnvironmentVariable方法对系统环境变量没有影响。要以编程方式添加或修改系统环境变量,把它们添加到 HKEY_LOCAL_MACHINE \系统\ CurrentControlSet \控制\会话管理器\环境的注册表项,然后播放了 WM_SETTINGCHANGE 消息的lParam 设置为字符串环境。这使得应用程序,如外壳,拿起您的更新。

Calling SetEnvironmentVariable has no effect on the system environment variables. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates.

所以,你需要写信给您已经试图写入注册表设置。然后播出 WM_SETTINGCHANGE 信息详见上文。您将需要与提升的权限,以便为这个成功运行。

So, you need to write to the registry setting that you are already attempting to write to. And then broadcast a WM_SETTINGCHANGE message as detailed above. You will need to be running with elevated rights in order for this to succeed.

一些例如code:

using Microsoft.Win32;
using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        const int HWND_BROADCAST = 0xffff;
        const uint WM_SETTINGCHANGE = 0x001a;

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, 
            UIntPtr wParam, string lParam);

        static void Main(string[] args)
        {
            using (var envKey = Registry.LocalMachine.OpenSubKey(
                @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment",
                true))
            {
                Contract.Assert(envKey != null, @"registry key is missing!");
                envKey.SetValue("TEST1", "TestValue");
                SendNotifyMessage((IntPtr)HWND_BROADCAST, WM_SETTINGCHANGE,
                    (UIntPtr)0, "Environment");
            }
        }
    }
}

不过,虽然这code不工作,.NET Framework提供的功能,更简单地多执行相同的任务。

However, whilst this code does work, the .net framework provides functionality to perform the same task much more simply.

Environment.SetEnvironmentVariable("TEST1", "TestValue", 
    EnvironmentVariableTarget.Machine);

三个参数的文档 Environment.SetEnvironmentVariable 过载说:

如果目标是EnvironmentVariableTarget.Machine,环境变量存储在HKEY_LOCAL_MACHINE \ SYSTEM \本地计算机的注册表中的ControlSet001 \控制\会话管理器\环境的关键。它也被复制到文件管理器的所有实例。环境变量,然后由从文件浏览器推出任何新的进程继承。

If target is EnvironmentVariableTarget.Machine, the environment variable is stored in the HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment key of the local computer's registry. It is also copied to all instances of File Explorer. The environment variable is then inherited by any new processes that are launched from File Explorer.

如果目标用户或计算机,其他应用程序由Windows WM_SETTINGCHANGE消息通知的设置操作的。

If target is User or Machine, other applications are notified of the set operation by a Windows WM_SETTINGCHANGE message.