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 | function GetContext: TRegistryContext;
|
---|
29 | procedure SetContext(AValue: TRegistryContext);
|
---|
30 | public
|
---|
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 ReadFloatWithDefault(const Name: string;
|
---|
36 | DefaultValue: Double): Double;
|
---|
37 | function DeleteKeyRecursive(const Key: string): Boolean;
|
---|
38 | function OpenKey(const Key: string; CanCreate: Boolean): Boolean;
|
---|
39 | property Context: TRegistryContext read GetContext write SetContext;
|
---|
40 | end;
|
---|
41 |
|
---|
42 | function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
|
---|
43 |
|
---|
44 |
|
---|
45 | implementation
|
---|
46 |
|
---|
47 | function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
|
---|
48 | begin
|
---|
49 | Result.RootKey := RootKey;
|
---|
50 | Result.Key := Key;
|
---|
51 | end;
|
---|
52 |
|
---|
53 | { TRegistryEx }
|
---|
54 |
|
---|
55 | function TRegistryEx.ReadIntegerWithDefault(const Name: string;
|
---|
56 | DefaultValue: Integer): Integer;
|
---|
57 | begin
|
---|
58 | if ValueExists(Name) then Result := ReadInteger(Name)
|
---|
59 | else begin
|
---|
60 | WriteInteger(Name, DefaultValue);
|
---|
61 | Result := DefaultValue;
|
---|
62 | end;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | function TRegistryEx.ReadStringWithDefault(const Name: string;
|
---|
66 | DefaultValue: string): string;
|
---|
67 | begin
|
---|
68 | if ValueExists(Name) then Result := ReadString(Name)
|
---|
69 | else begin
|
---|
70 | WriteString(Name, DefaultValue);
|
---|
71 | Result := DefaultValue;
|
---|
72 | end;
|
---|
73 | end;
|
---|
74 |
|
---|
75 | function TRegistryEx.ReadFloatWithDefault(const Name: string;
|
---|
76 | DefaultValue: Double): Double;
|
---|
77 | begin
|
---|
78 | if ValueExists(Name) then Result := ReadFloat(Name)
|
---|
79 | else begin
|
---|
80 | WriteFloat(Name, DefaultValue);
|
---|
81 | Result := DefaultValue;
|
---|
82 | end;
|
---|
83 | end;
|
---|
84 |
|
---|
85 | function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
|
---|
86 | var
|
---|
87 | SubKeys: TStringList;
|
---|
88 | I: Integer;
|
---|
89 | begin
|
---|
90 | try
|
---|
91 | SubKeys := TStringList.Create;
|
---|
92 | if OpenKey(Key, False) and HasSubKeys then begin
|
---|
93 | GetKeyNames(SubKeys);
|
---|
94 | for I := 0 to SubKeys.Count - 1 do
|
---|
95 | DeleteKeyRecursive(Key + '\' + SubKeys[I]);
|
---|
96 | end;
|
---|
97 | Result := DeleteKey(Key);
|
---|
98 | finally
|
---|
99 | SubKeys.Free;
|
---|
100 | end;
|
---|
101 | end;
|
---|
102 |
|
---|
103 | function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
|
---|
104 | begin
|
---|
105 | {$IFDEF Linux}
|
---|
106 | CloseKey;
|
---|
107 | {$ENDIF}
|
---|
108 | Result := inherited OpenKey(Key, CanCreate);
|
---|
109 | end;
|
---|
110 |
|
---|
111 | function TRegistryEx.GetContext: TRegistryContext;
|
---|
112 | begin
|
---|
113 | Result.Key := CurrentPath;
|
---|
114 | Result.RootKey := RootKey;
|
---|
115 | end;
|
---|
116 |
|
---|
117 | procedure TRegistryEx.SetContext(AValue: TRegistryContext);
|
---|
118 | begin
|
---|
119 | RootKey := AValue.RootKey;
|
---|
120 | OpenKey(AValue.Key, True);
|
---|
121 | end;
|
---|
122 |
|
---|
123 | function TRegistryEx.ReadBoolWithDefault(const Name: string;
|
---|
124 | DefaultValue: Boolean): Boolean;
|
---|
125 | begin
|
---|
126 | if ValueExists(Name) then Result := ReadBool(Name)
|
---|
127 | else begin
|
---|
128 | WriteBool(Name, DefaultValue);
|
---|
129 | Result := DefaultValue;
|
---|
130 | end;
|
---|
131 | end;
|
---|
132 |
|
---|
133 | end.
|
---|