本文整理自网络,侵删。
function GetEnvVariable(Name: string; User: Boolean = True): string;var Str: array[0..255] of char;begin with TRegistry.Create do try if User then begin RootKey := HKEY_CURRENT_USER; //OpenKey('Environment', False); OpenKeyReadOnly('Environment', False); end else begin RootKey := HKEY_LOCAL_MACHINE; //OpenKey('SYSTEM\CurrentControlSet\Control\Session ' + // 'Manager\Environment', False); OpenKeyReadOnly('SYSTEM\CurrentControlSet\Control\Session ' + 'Manager\Environment', False); end; Result := ReadString(Name); ExpandEnvironmentStrings(PChar(Result), Str, 255); Result := Str; finally Free; end;end;
procedure SetEnvVariable(Name, Value: string; User: Boolean = True);var rv: DWORD;begin with TRegistry.Create do try if User then begin RootKey := HKEY_CURRENT_USER; OpenKey('Environment', False); WriteString(Name, Value); end else begin RootKey := HKEY_LOCAL_MACHINE; OpenKey('SYSTEM\CurrentControlSet\Control\Session ' + 'Manager\Environment', False); end; WriteString(Name, Value); SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, LParam (PChar('Environment')), SMTO_ABORTIFHUNG, 5000, rv); finally Free; end;end;
相关阅读 >>
Delphi fdconnection查看所有表包含用户表和系统表
Delphi 检查程序是否在(vm,vpc等)虚拟机运行 Delphi(测试可用)
Delphi 用idhttp打开网页或下载文件时如何显示进度
更多相关阅读请进入《Delphi》频道 >>