首页  编辑  

设置环境变量

Tags: /超级猛料/OS.操作系统/   Date Created:

如何永久改变环境变量?

答:首先,你必须修改注册表HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment,在下面添加你的环境变量,然后广播发送一个 WM_SETTINGCHANGE 即可。

{*********************************************}

{ Set Global Environment Function             }

{ Coder : Kingron,2002.8.6                    }

{ Bug Report : Kingron@163.net                }

{ Test OK For Windows 2000 Advance Server     }

{ Parameter:                                  }

{ Name : The environment name                 }

{ Value: The environment Value                }

{ Ex: SetGlobalEnvironment('MyVar','OK')      }

{*********************************************}

function SetGlobalEnvironment ( const Name , Value : string ; const User : Boolean = True ): boolean ;

resourcestring

 REG_MACHINE_LOCATION = 'System\CurrentControlSet\Control\Session Manager\Environment' ;

 REG_USER_LOCATION = 'Environment' ;

begin

  with TRegistry . Create do

  try

    if User then { User Environment Variable }

     Result := OpenKey ( REG_USER_LOCATION , True )

    else { System Environment Variable }

    begin

     RootKey := HKEY_LOCAL_MACHINE ;

     Result := OpenKey ( REG_MACHINE_LOCATION , True );

    end ;

    if Result then

    begin

     WriteString ( Name , Value ); { Write Registry for Global Environment }

      { Update Current Process Environment Variable }

     SetEnvironmentVariable ( pchar ( Name ), pchar ( Value ));

      { Send Message To All Top Window for Refresh }

     SendMessage ( HWND_BROADCAST , WM_SETTINGCHANGE , 0 , integer ( Pchar ( 'Environment' )));

    end ;

  finally

   Free ;

  end ;

end ; { SetGlobalEnvironment }