| 1 | unit RegistryEx;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, Registry;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | TRegistryRoot = (rrKeyClassesRoot, rrKeyCurrentUser, rrKeyLocalMachine,
|
|---|
| 10 | rrKeyUsers, rrKeyPerformanceData, rrKeyCurrentConfig, rrKeyDynData);
|
|---|
| 11 |
|
|---|
| 12 | { TRegistryContext }
|
|---|
| 13 |
|
|---|
| 14 | TRegistryContext = record
|
|---|
| 15 | RootKey: HKEY;
|
|---|
| 16 | Key: string;
|
|---|
| 17 | class function Create(RootKey: TRegistryRoot; Key: string): TRegistryContext; static; overload;
|
|---|
| 18 | class function Create(RootKey: HKEY; Key: string): TRegistryContext; static; overload;
|
|---|
| 19 | class operator Equal(A, B: TRegistryContext): Boolean;
|
|---|
| 20 | end;
|
|---|
| 21 |
|
|---|
| 22 | { TRegistryEx }
|
|---|
| 23 |
|
|---|
| 24 | TRegistryEx = class(TRegistry)
|
|---|
| 25 | private
|
|---|
| 26 | function GetCurrentContext: TRegistryContext;
|
|---|
| 27 | procedure SetCurrentContext(AValue: TRegistryContext);
|
|---|
| 28 | public
|
|---|
| 29 | function ReadChar(const Name: string): Char;
|
|---|
| 30 | procedure WriteChar(const Name: string; Value: Char);
|
|---|
| 31 | function ReadBoolWithDefault(const Name: string;
|
|---|
| 32 | DefaultValue: Boolean): Boolean;
|
|---|
| 33 | function ReadIntegerWithDefault(const Name: string; DefaultValue: Integer): Integer;
|
|---|
| 34 | function ReadStringWithDefault(const Name: string; DefaultValue: string): string;
|
|---|
| 35 | function ReadCharWithDefault(const Name: string; DefaultValue: Char): Char;
|
|---|
| 36 | function ReadFloatWithDefault(const Name: string;
|
|---|
| 37 | DefaultValue: Double): Double;
|
|---|
| 38 | function ReadDateTimeWithDefault(const Name: string; DefaultValue: TDateTime): TDateTime;
|
|---|
| 39 | function DeleteKeyRecursive(const Key: string): Boolean;
|
|---|
| 40 | function OpenKey(const Key: string; CanCreate: Boolean): Boolean;
|
|---|
| 41 | property CurrentContext: TRegistryContext read GetCurrentContext write SetCurrentContext;
|
|---|
| 42 | end;
|
|---|
| 43 |
|
|---|
| 44 | const
|
|---|
| 45 | RegistryRootHKEY: array[TRegistryRoot] of HKEY = (HKEY_CLASSES_ROOT,
|
|---|
| 46 | HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_PERFORMANCE_DATA,
|
|---|
| 47 | HKEY_CURRENT_CONFIG, HKEY_DYN_DATA);
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | implementation
|
|---|
| 51 |
|
|---|
| 52 | { TRegistryContext }
|
|---|
| 53 |
|
|---|
| 54 | class operator TRegistryContext.Equal(A, B: TRegistryContext): Boolean;
|
|---|
| 55 | begin
|
|---|
| 56 | Result := (A.Key = B.Key) and (A.RootKey = B.RootKey);
|
|---|
| 57 | end;
|
|---|
| 58 |
|
|---|
| 59 | class function TRegistryContext.Create(RootKey: TRegistryRoot; Key: string): TRegistryContext;
|
|---|
| 60 | begin
|
|---|
| 61 | Result.RootKey := RegistryRootHKEY[RootKey];
|
|---|
| 62 | Result.Key := Key;
|
|---|
| 63 | end;
|
|---|
| 64 |
|
|---|
| 65 | class function TRegistryContext.Create(RootKey: HKEY; Key: string): TRegistryContext;
|
|---|
| 66 | begin
|
|---|
| 67 | Result.RootKey := RootKey;
|
|---|
| 68 | Result.Key := Key;
|
|---|
| 69 | end;
|
|---|
| 70 |
|
|---|
| 71 | { TRegistryEx }
|
|---|
| 72 |
|
|---|
| 73 | function TRegistryEx.ReadIntegerWithDefault(const Name: string;
|
|---|
| 74 | DefaultValue: Integer): Integer;
|
|---|
| 75 | begin
|
|---|
| 76 | if ValueExists(Name) then Result := ReadInteger(Name)
|
|---|
| 77 | else begin
|
|---|
| 78 | WriteInteger(Name, DefaultValue);
|
|---|
| 79 | Result := DefaultValue;
|
|---|
| 80 | end;
|
|---|
| 81 | end;
|
|---|
| 82 |
|
|---|
| 83 | function TRegistryEx.ReadStringWithDefault(const Name: string;
|
|---|
| 84 | DefaultValue: string): string;
|
|---|
| 85 | begin
|
|---|
| 86 | if ValueExists(Name) then Result := ReadString(Name)
|
|---|
| 87 | else begin
|
|---|
| 88 | WriteString(Name, DefaultValue);
|
|---|
| 89 | Result := DefaultValue;
|
|---|
| 90 | end;
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | function TRegistryEx.ReadCharWithDefault(const Name: string; DefaultValue: Char
|
|---|
| 94 | ): Char;
|
|---|
| 95 | begin
|
|---|
| 96 | if ValueExists(Name) then Result := ReadChar(Name)
|
|---|
| 97 | else begin
|
|---|
| 98 | WriteChar(Name, DefaultValue);
|
|---|
| 99 | Result := DefaultValue;
|
|---|
| 100 | end;
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | function TRegistryEx.ReadFloatWithDefault(const Name: string;
|
|---|
| 104 | DefaultValue: Double): Double;
|
|---|
| 105 | begin
|
|---|
| 106 | if ValueExists(Name) then Result := ReadFloat(Name)
|
|---|
| 107 | else begin
|
|---|
| 108 | WriteFloat(Name, DefaultValue);
|
|---|
| 109 | Result := DefaultValue;
|
|---|
| 110 | end;
|
|---|
| 111 | end;
|
|---|
| 112 |
|
|---|
| 113 | function TRegistryEx.ReadDateTimeWithDefault(const Name: string;
|
|---|
| 114 | DefaultValue: TDateTime): TDateTime;
|
|---|
| 115 | begin
|
|---|
| 116 | if ValueExists(Name) then Result := ReadDateTime(Name)
|
|---|
| 117 | else begin
|
|---|
| 118 | WriteDateTime(Name, DefaultValue);
|
|---|
| 119 | Result := DefaultValue;
|
|---|
| 120 | end;
|
|---|
| 121 | end;
|
|---|
| 122 |
|
|---|
| 123 | function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
|
|---|
| 124 | var
|
|---|
| 125 | SubKeys: TStringList;
|
|---|
| 126 | I: Integer;
|
|---|
| 127 | begin
|
|---|
| 128 | try
|
|---|
| 129 | SubKeys := TStringList.Create;
|
|---|
| 130 | if OpenKey(Key, False) and HasSubKeys then begin
|
|---|
| 131 | GetKeyNames(SubKeys);
|
|---|
| 132 | for I := 0 to SubKeys.Count - 1 do
|
|---|
| 133 | DeleteKeyRecursive(Key + '\' + SubKeys[I]);
|
|---|
| 134 | end;
|
|---|
| 135 | Result := DeleteKey(Key);
|
|---|
| 136 | finally
|
|---|
| 137 | SubKeys.Free;
|
|---|
| 138 | end;
|
|---|
| 139 | end;
|
|---|
| 140 |
|
|---|
| 141 | function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
|
|---|
| 142 | begin
|
|---|
| 143 | {$IFDEF UNIX}
|
|---|
| 144 | //CloseKey;
|
|---|
| 145 | {$ENDIF}
|
|---|
| 146 | Result := inherited;
|
|---|
| 147 | end;
|
|---|
| 148 |
|
|---|
| 149 | function TRegistryEx.GetCurrentContext: TRegistryContext;
|
|---|
| 150 | begin
|
|---|
| 151 | Result.Key := String(CurrentPath);
|
|---|
| 152 | Result.RootKey := RootKey;
|
|---|
| 153 | end;
|
|---|
| 154 |
|
|---|
| 155 | procedure TRegistryEx.SetCurrentContext(AValue: TRegistryContext);
|
|---|
| 156 | begin
|
|---|
| 157 | RootKey := AValue.RootKey;
|
|---|
| 158 | OpenKey(AValue.Key, True);
|
|---|
| 159 | end;
|
|---|
| 160 |
|
|---|
| 161 | function TRegistryEx.ReadChar(const Name: string): Char;
|
|---|
| 162 | var
|
|---|
| 163 | S: string;
|
|---|
| 164 | begin
|
|---|
| 165 | S := ReadString(Name);
|
|---|
| 166 | if Length(S) > 0 then Result := S[1]
|
|---|
| 167 | else Result := #0;
|
|---|
| 168 | end;
|
|---|
| 169 |
|
|---|
| 170 | procedure TRegistryEx.WriteChar(const Name: string; Value: Char);
|
|---|
| 171 | begin
|
|---|
| 172 | WriteString(Name, Value);
|
|---|
| 173 | end;
|
|---|
| 174 |
|
|---|
| 175 | function TRegistryEx.ReadBoolWithDefault(const Name: string;
|
|---|
| 176 | DefaultValue: Boolean): Boolean;
|
|---|
| 177 | begin
|
|---|
| 178 | if ValueExists(Name) then Result := ReadBool(Name)
|
|---|
| 179 | else begin
|
|---|
| 180 | WriteBool(Name, DefaultValue);
|
|---|
| 181 | Result := DefaultValue;
|
|---|
| 182 | end;
|
|---|
| 183 | end;
|
|---|
| 184 |
|
|---|
| 185 | end.
|
|---|