程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 讀寫系統/用戶環境變量,讀寫用戶環境變量

讀寫系統/用戶環境變量,讀寫用戶環境變量

編輯:Delphi

讀寫系統/用戶環境變量,讀寫用戶環境變量


 
轉自: 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;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved