每次写程序涉及注册表操作时都要创建TRegistry类,感觉太麻烦了,直接写了个单元,引用后就能通用接口函数直接操作注册表,欢迎讨论。unit uRegistry;interfaceuses
Classes;type
TRegistryRootType = (rtClassesRoot, rtCurrentUser, rtLocaleMachine,
rtUsers, rtCurrentConfig);//读取字符串数值
function RegistryGetValueString(ARootType: TRegistryRootType;
APath: string; AKeyName: string) : string;//读取整数数值
function RegistryGetValueInteger(ARootType: TRegistryRootType;
APath: string; AKeyName: string) : Integer;//读取键列表
procedure RegistryGetKeyNames(ARootType: TRegistryRootType;
APath: string; ANames: TStrings);//读取键值列表
procedure RegistryGetValueNames(ARootType: TRegistryRootType;
APath: string; ANames: TStrings);//写入字符串数值
procedure RegistrySetValueString(ARootType: TRegistryRootType;
APath: string; AKeyName: string; AValue : string);//写入整数数值
procedure RegistrySetValueInteger(ARootType: TRegistryRootType;
APath: string; AKeyName: string; AValue : Integer);//创建键
function RegistryCreateKey(ARootType: TRegistryRootType;
AKeyName: string) : Boolean;//删除键
function RegistryDeleteKey(ARootType: TRegistryRootType;
AKeyName: string) : Boolean;
implementationuses
Registry, Windows;var
RegCtl : TRegistry;procedure SetRootType(ARootType: TRegistryRootType);
begin
with RegCtl do
begin
case ARootType of
rtClassesRoot: RootKey := HKEY_CLASSES_ROOT;
rtCurrentUser: RootKey := HKEY_CURRENT_USER;
rtLocaleMachine: RootKey := HKEY_LOCAL_MACHINE;
rtUsers: RootKey := HKEY_USERS;
rtCurrentConfig: RootKey := HKEY_CURRENT_CONFIG;
end;
end;
end;function RegistryGetValueString(ARootType: TRegistryRootType;
APath: string; AKeyName: string) : string;
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
Result := RegCtl.ReadString(AKeyName);
RegCtl.CloseKey;
end;function RegistryGetValueInteger(ARootType: TRegistryRootType;
APath: string; AKeyName: string) : Integer;
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
Result := RegCtl.ReadInteger(AKeyName);
RegCtl.CloseKey;
end;procedure RegistryGetKeyNames(ARootType: TRegistryRootType;
APath: string; ANames: TStrings);
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
RegCtl.GetKeyNames(ANames);
RegCtl.CloseKey;
end;procedure RegistryGetValueNames(ARootType: TRegistryRootType;
APath: string; ANames: TStrings);
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
RegCtl.GetValueNames(ANames);
RegCtl.CloseKey;
end;procedure RegistrySetValueString(ARootType: TRegistryRootType;
APath: string; AKeyName: string; AValue : string);
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
RegCtl.WriteString(AKeyName, AValue);
RegCtl.CloseKey;
end;procedure RegistrySetValueInteger(ARootType: TRegistryRootType;
APath: string; AKeyName: string; AValue : Integer);
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
RegCtl.WriteInteger(AKeyName, AValue);
RegCtl.CloseKey;
end;function RegistryCreateKey(ARootType: TRegistryRootType;
AKeyName: string) : Boolean;
begin
SetRootType(ARootType);
Result := RegCtl.CreateKey(AKeyName);
end;function RegistryDeleteKey(ARootType: TRegistryRootType;
AKeyName: string) : Boolean;
begin
SetRootType(ARootType);
Result := RegCtl.DeleteKey(AKeyName);
end;initialization
RegCtl := TRegistry.Create;finalization
RegCtl.Free;end.
Classes;type
TRegistryRootType = (rtClassesRoot, rtCurrentUser, rtLocaleMachine,
rtUsers, rtCurrentConfig);//读取字符串数值
function RegistryGetValueString(ARootType: TRegistryRootType;
APath: string; AKeyName: string) : string;//读取整数数值
function RegistryGetValueInteger(ARootType: TRegistryRootType;
APath: string; AKeyName: string) : Integer;//读取键列表
procedure RegistryGetKeyNames(ARootType: TRegistryRootType;
APath: string; ANames: TStrings);//读取键值列表
procedure RegistryGetValueNames(ARootType: TRegistryRootType;
APath: string; ANames: TStrings);//写入字符串数值
procedure RegistrySetValueString(ARootType: TRegistryRootType;
APath: string; AKeyName: string; AValue : string);//写入整数数值
procedure RegistrySetValueInteger(ARootType: TRegistryRootType;
APath: string; AKeyName: string; AValue : Integer);//创建键
function RegistryCreateKey(ARootType: TRegistryRootType;
AKeyName: string) : Boolean;//删除键
function RegistryDeleteKey(ARootType: TRegistryRootType;
AKeyName: string) : Boolean;
implementationuses
Registry, Windows;var
RegCtl : TRegistry;procedure SetRootType(ARootType: TRegistryRootType);
begin
with RegCtl do
begin
case ARootType of
rtClassesRoot: RootKey := HKEY_CLASSES_ROOT;
rtCurrentUser: RootKey := HKEY_CURRENT_USER;
rtLocaleMachine: RootKey := HKEY_LOCAL_MACHINE;
rtUsers: RootKey := HKEY_USERS;
rtCurrentConfig: RootKey := HKEY_CURRENT_CONFIG;
end;
end;
end;function RegistryGetValueString(ARootType: TRegistryRootType;
APath: string; AKeyName: string) : string;
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
Result := RegCtl.ReadString(AKeyName);
RegCtl.CloseKey;
end;function RegistryGetValueInteger(ARootType: TRegistryRootType;
APath: string; AKeyName: string) : Integer;
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
Result := RegCtl.ReadInteger(AKeyName);
RegCtl.CloseKey;
end;procedure RegistryGetKeyNames(ARootType: TRegistryRootType;
APath: string; ANames: TStrings);
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
RegCtl.GetKeyNames(ANames);
RegCtl.CloseKey;
end;procedure RegistryGetValueNames(ARootType: TRegistryRootType;
APath: string; ANames: TStrings);
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
RegCtl.GetValueNames(ANames);
RegCtl.CloseKey;
end;procedure RegistrySetValueString(ARootType: TRegistryRootType;
APath: string; AKeyName: string; AValue : string);
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
RegCtl.WriteString(AKeyName, AValue);
RegCtl.CloseKey;
end;procedure RegistrySetValueInteger(ARootType: TRegistryRootType;
APath: string; AKeyName: string; AValue : Integer);
begin
SetRootType(ARootType);
RegCtl.OpenKey(APath, False);
RegCtl.WriteInteger(AKeyName, AValue);
RegCtl.CloseKey;
end;function RegistryCreateKey(ARootType: TRegistryRootType;
AKeyName: string) : Boolean;
begin
SetRootType(ARootType);
Result := RegCtl.CreateKey(AKeyName);
end;function RegistryDeleteKey(ARootType: TRegistryRootType;
AKeyName: string) : Boolean;
begin
SetRootType(ARootType);
Result := RegCtl.DeleteKey(AKeyName);
end;initialization
RegCtl := TRegistry.Create;finalization
RegCtl.Free;end.
就像把API重新封装一样
你只不过将类进行上一层的封装
有道理,当时我也这么想,但是没有找到是否能判定当前KEY是否已经打开的方法,所以就这样了。有待改进。