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