ale*_*555 7 .net c# environment-variables
我正在尝试在我的应用程序中设置系统环境变量,但得到一个SecurityException.我测试了我在谷歌找到的所有内容 - 没有成功.这是我的代码(注意,我是我的电脑的管理员,并以管理员身份运行VS2012):
尝试1
new EnvironmentPermission(EnvironmentPermissionAccess.Write, "TEST1").Demand();
Environment.SetEnvironmentVariable("TEST1", "MyTest", EnvironmentVariableTarget.Machine);
Run Code Online (Sandbox Code Playgroud)
尝试2
new EnvironmentPermission(EnvironmentPermissionAccess.Write, "TEST1").Demand();
using (var envKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true))
{
Contract.Assert(envKey != null, @"HKLM\System\CurrentControlSet\Control\Session Manager\Environment is missing!");
envKey.SetValue("TEST1", "TestValue");
}
Run Code Online (Sandbox Code Playgroud)
你有什么其他的建议?
Dav*_*nan 23
该文件会告诉你如何做到这一点.
调用
SetEnvironmentVariable对系统环境变量没有影响.要以编程方式添加或修改系统环境变量,请将它们添加到HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment注册表项,然后将设置为字符串的WM_SETTINGCHANGE消息广播.这允许应用程序(如shell)获取更新.lParam"Environment"
因此,您需要写入您已尝试写入的注册表设置.然后WM_SETTINGCHANGE如上所述广播消息.您需要以提升的权限运行才能成功.
一些示例代码:
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");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,虽然此代码确实有效,但.net框架提供了更简单地执行相同任务的功能.
Environment.SetEnvironmentVariable("TEST1", "TestValue",
EnvironmentVariableTarget.Machine);
Run Code Online (Sandbox Code Playgroud)
三个参数重载的文档Environment.SetEnvironmentVariable说:
如果target是EnvironmentVariableTarget.Machine,则环境变量存储在本地计算机注册表的HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment项中.它也会复制到文件资源管理器的所有实例中.然后,环境变量将由从文件资源管理器启动的任何新进程继承.
如果target是User或Machine,则通过Windows WM_SETTINGCHANGE消息向其他应用程序通知设置操作.