| 1 | unit URegistry;
|
|---|
| 2 |
|
|---|
| 3 | {$MODE Delphi}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, Registry;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 | TRegistryRoot = (rrKeyClassesRoot = HKEY($80000000),
|
|---|
| 12 | rrKeyCurrentUser = HKEY($80000001),
|
|---|
| 13 | rrKeyLocalMachine = HKEY($80000002),
|
|---|
| 14 | rrKeyUsers = HKEY($80000003),
|
|---|
| 15 | rrKeyPerformanceData = HKEY($80000004),
|
|---|
| 16 | rrKeyCurrentConfig = HKEY($80000005),
|
|---|
| 17 | rrKeyDynData = HKEY($80000006));
|
|---|
| 18 |
|
|---|
| 19 | TRegistryContext = record
|
|---|
| 20 | RootKey: HKEY;
|
|---|
| 21 | Key: string;
|
|---|
| 22 | end;
|
|---|
| 23 |
|
|---|
| 24 | { TRegistryEx }
|
|---|
| 25 |
|
|---|
| 26 | TRegistryEx = class(TRegistry)
|
|---|
| 27 | private
|
|---|
| 28 | public
|
|---|
| 29 | function ReadBoolWithDefault(const Name: string;
|
|---|
| 30 | DefaultValue: Boolean): Boolean;
|
|---|
| 31 | function ReadIntegerWithDefault(const Name: string; DefaultValue: Integer): Integer;
|
|---|
| 32 | function ReadStringWithDefault(const Name: string; DefaultValue: string): string;
|
|---|
| 33 | function ReadFloatWithDefault(const Name: string;
|
|---|
| 34 | DefaultValue: Double): Double;
|
|---|
| 35 | function DeleteKeyRecursive(const Key: string): Boolean;
|
|---|
| 36 | end;
|
|---|
| 37 |
|
|---|
| 38 | function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | implementation
|
|---|
| 42 |
|
|---|
| 43 | function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
|
|---|
| 44 | begin
|
|---|
| 45 | Result.RootKey := RootKey;
|
|---|
| 46 | Result.Key := Key;
|
|---|
| 47 | end;
|
|---|
| 48 |
|
|---|
| 49 | { TRegistryEx }
|
|---|
| 50 |
|
|---|
| 51 | function TRegistryEx.ReadIntegerWithDefault(const Name: string;
|
|---|
| 52 | DefaultValue: Integer): Integer;
|
|---|
| 53 | begin
|
|---|
| 54 | if ValueExists(Name) then Result := ReadInteger(Name)
|
|---|
| 55 | else begin
|
|---|
| 56 | WriteInteger(Name, DefaultValue);
|
|---|
| 57 | Result := DefaultValue;
|
|---|
| 58 | end;
|
|---|
| 59 | end;
|
|---|
| 60 |
|
|---|
| 61 | function TRegistryEx.ReadStringWithDefault(const Name: string;
|
|---|
| 62 | DefaultValue: string): string;
|
|---|
| 63 | begin
|
|---|
| 64 | if ValueExists(Name) then Result := ReadString(Name)
|
|---|
| 65 | else begin
|
|---|
| 66 | WriteString(Name, DefaultValue);
|
|---|
| 67 | Result := DefaultValue;
|
|---|
| 68 | end;
|
|---|
| 69 | end;
|
|---|
| 70 |
|
|---|
| 71 | function TRegistryEx.ReadFloatWithDefault(const Name: string;
|
|---|
| 72 | DefaultValue: Double): Double;
|
|---|
| 73 | begin
|
|---|
| 74 | if ValueExists(Name) then Result := ReadFloat(Name)
|
|---|
| 75 | else begin
|
|---|
| 76 | WriteFloat(Name, DefaultValue);
|
|---|
| 77 | Result := DefaultValue;
|
|---|
| 78 | end;
|
|---|
| 79 | end;
|
|---|
| 80 |
|
|---|
| 81 | function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
|
|---|
| 82 | var
|
|---|
| 83 | SubKeys: TStringList;
|
|---|
| 84 | I: Integer;
|
|---|
| 85 | begin
|
|---|
| 86 | try
|
|---|
| 87 | SubKeys := TStringList.Create;
|
|---|
| 88 | if OpenKey(Key, False) and HasSubKeys then begin
|
|---|
| 89 | GetKeyNames(SubKeys);
|
|---|
| 90 | for I := 0 to SubKeys.Count - 1 do
|
|---|
| 91 | DeleteKeyRecursive(Key + '\' + SubKeys[I]);
|
|---|
| 92 | end;
|
|---|
| 93 | Result := DeleteKey(Key);
|
|---|
| 94 | finally
|
|---|
| 95 | SubKeys.Free;
|
|---|
| 96 | end;
|
|---|
| 97 | end;
|
|---|
| 98 |
|
|---|
| 99 | function TRegistryEx.ReadBoolWithDefault(const Name: string;
|
|---|
| 100 | DefaultValue: Boolean): Boolean;
|
|---|
| 101 | begin
|
|---|
| 102 | if ValueExists(Name) then Result := ReadBool(Name)
|
|---|
| 103 | else begin
|
|---|
| 104 | WriteBool(Name, DefaultValue);
|
|---|
| 105 | Result := DefaultValue;
|
|---|
| 106 | end;
|
|---|
| 107 | end;
|
|---|
| 108 |
|
|---|
| 109 | end.
|
|---|