码迷,mamicode.com
首页 > 其他好文 > 详细

读写系统/用户环境变量

时间:2014-11-15 18:44:32      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   使用   sp   

 
转自: http://delphi.cjcsoft.net/viewthread.php?tid=43647
使用 SetEnvironmentVariable 和 GetEnvironmentVariable 似乎是
只能对当前进程环境设置环境变量,所以设置后没有能在系统设置里看到,用下列
设置注册表的方法则可以全局设置系统环境变量或者用户环境变量,再发送一条系
统广播通知,达到立即生效的目的.
Reading and Writing System-Wide Environment Variables.

Title: Reading and Writing System-Wide Environment Variables.

Question: How do you set an environment variable that will apply outside the process that set the variable or those spawned by it?

Answer:
On Windows 2000, if you open the control panel and double click on 
the system icon, the system properties dialog box will open.  On the 
"Advanced" tab, you can click the "Environment Variables" tab to see 
a list of the user and system environment variables. The procedures 
and functions below allow you to read and write those variables.

It is worth mentioning that you can also use "GetEnvironmentVariable" 
and "SetEnvironmentVariable" to read and write environment variables.  
However, if you set and environment variable with 
"SetEnvironmentVariable", the value you set applies only to the 
process that called "SetEnvironmentVariable" or are spawned by it.

The first two procedures read and write environment variables for the 
current user.

function GetUserEnvironmentVariable(const name: string): string;
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_CURRENT_USER;
    OpenKey(Environment, False);
    result := ReadString(name);
  finally
    Free
  end
end;

procedure SetUserEnvironmentVariable(const name, value: string);
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_CURRENT_USER;
    OpenKey(Environment, False);
    WriteString(name, value);
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, LParam
      (PChar(Environment)), SMTO_ABORTIFHUNG, 5000, rv);
  finally
    Free
  end
end;


The next two procedures read and write environment variables for the 
system and thus affect all users.

function GetSystemEnvironmentVariable(const name: string): string;
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_LOCAL_MACHINE;
    OpenKey(SYSTEM\CurrentControlSet\Control\Session  + 
      Manager\Environment, False);
    result := ReadString(name);
  finally
    Free
  end
end;

// Modified from
// http://www.delphiabc.com/TipNo.asp?ID=117
// The original article did not include the space in 
// "Session Manager" which caused the procedure to fail.

procedure SetSystemEnvironmentVariable(const name, value: string);
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_LOCAL_MACHINE;
    OpenKey(SYSTEM\CurrentControlSet\Control\Session  + 
      Manager\Environment, False);
    WriteString(name, value);
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, LParam
      (PChar(Environment)), SMTO_ABORTIFHUNG, 5000, rv);
  finally
    Free
  end
end;

读写系统/用户环境变量

标签:style   blog   http   io   color   ar   os   使用   sp   

原文地址:http://www.cnblogs.com/nikosoft/p/4099955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!