本文整理自网络,侵删。
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;
相关阅读 >>
winapi 字符及字符串函数(8): ischarupper - 是否是个大写字母
Delphi 利用tcomm组件 spcomm 实现串行通信
Delphi idhttp returning http 1.1/500 显示错误内容
Delphi 用 superobject 解析淘宝上的 json 数据
更多相关阅读请进入《Delphi》频道 >>