1 | unit URegistry;
|
---|
2 |
|
---|
3 | {$MODE Delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, MyRegistry;
|
---|
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, B: TRegistryContext): Boolean;
|
---|
25 | end;
|
---|
26 |
|
---|
27 | { TRegistryEx }
|
---|
28 |
|
---|
29 | TRegistryEx = class(TRegistry)
|
---|
30 | private
|
---|
31 | function GetCurrentContext: TRegistryContext;
|
---|
32 | procedure SetCurrentContext(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 CurrentContext: TRegistryContext read GetCurrentContext write SetCurrentContext;
|
---|
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, B: TRegistryContext): Boolean;
|
---|
59 | begin
|
---|
60 | Result := (A.Key = B.Key) and (A.RootKey = B.RootKey);
|
---|
61 | end;
|
---|
62 |
|
---|
63 | { TRegistryEx }
|
---|
64 |
|
---|
65 | function TRegistryEx.ReadIntegerWithDefault(const Name: string;
|
---|
66 | DefaultValue: Integer): Integer;
|
---|
67 | begin
|
---|
68 | if ValueExists(Name) then Result := ReadInteger(Name)
|
---|
69 | else begin
|
---|
70 | WriteInteger(Name, DefaultValue);
|
---|
71 | Result := DefaultValue;
|
---|
72 | end;
|
---|
73 | end;
|
---|
74 |
|
---|
75 | function TRegistryEx.ReadStringWithDefault(const Name: string;
|
---|
76 | DefaultValue: string): string;
|
---|
77 | begin
|
---|
78 | if ValueExists(Name) then Result := ReadString(Name)
|
---|
79 | else begin
|
---|
80 | WriteString(Name, DefaultValue);
|
---|
81 | Result := DefaultValue;
|
---|
82 | end;
|
---|
83 | end;
|
---|
84 |
|
---|
85 | function TRegistryEx.ReadFloatWithDefault(const Name: string;
|
---|
86 | DefaultValue: Double): Double;
|
---|
87 | begin
|
---|
88 | if ValueExists(Name) then Result := ReadFloat(Name)
|
---|
89 | else begin
|
---|
90 | WriteFloat(Name, DefaultValue);
|
---|
91 | Result := DefaultValue;
|
---|
92 | end;
|
---|
93 | end;
|
---|
94 |
|
---|
95 | function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
|
---|
96 | var
|
---|
97 | SubKeys: TStringList;
|
---|
98 | I: Integer;
|
---|
99 | begin
|
---|
100 | try
|
---|
101 | SubKeys := TStringList.Create;
|
---|
102 | if OpenKey(Key, False) and HasSubKeys then begin
|
---|
103 | GetKeyNames(SubKeys);
|
---|
104 | for I := 0 to SubKeys.Count - 1 do
|
---|
105 | DeleteKeyRecursive(Key + '\' + SubKeys[I]);
|
---|
106 | end;
|
---|
107 | Result := DeleteKey(Key);
|
---|
108 | finally
|
---|
109 | SubKeys.Free;
|
---|
110 | end;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
|
---|
114 | begin
|
---|
115 | {$IFDEF Linux}
|
---|
116 | CloseKey;
|
---|
117 | {$ENDIF}
|
---|
118 | Result := inherited OpenKey(Key, CanCreate);
|
---|
119 | end;
|
---|
120 |
|
---|
121 | function TRegistryEx.GetCurrentContext: TRegistryContext;
|
---|
122 | begin
|
---|
123 | Result.Key := CurrentPath;
|
---|
124 | Result.RootKey := RootKey;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TRegistryEx.SetCurrentContext(AValue: TRegistryContext);
|
---|
128 | begin
|
---|
129 | RootKey := AValue.RootKey;
|
---|
130 | OpenKey(AValue.Key, True);
|
---|
131 | end;
|
---|
132 |
|
---|
133 | function TRegistryEx.ReadBoolWithDefault(const Name: string;
|
---|
134 | DefaultValue: Boolean): Boolean;
|
---|
135 | begin
|
---|
136 | if ValueExists(Name) then Result := ReadBool(Name)
|
---|
137 | else begin
|
---|
138 | WriteBool(Name, DefaultValue);
|
---|
139 | Result := DefaultValue;
|
---|
140 | end;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | end.
|
---|