source: trunk/Packages/Common/URegistry.pas

Last change on this file was 5, checked in by chronos, 12 years ago
  • Přidáno: Uchování nastavení spojení v registrech.
File size: 3.3 KB
Line 
1unit URegistry;
2
3{$MODE Delphi}
4
5interface
6
7uses
8 Classes, Registry;
9
10type
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
42function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
43
44
45implementation
46
47function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
48begin
49 Result.RootKey := RootKey;
50 Result.Key := Key;
51end;
52
53{ TRegistryEx }
54
55function TRegistryEx.ReadIntegerWithDefault(const Name: string;
56 DefaultValue: Integer): Integer;
57begin
58 if ValueExists(Name) then Result := ReadInteger(Name)
59 else begin
60 WriteInteger(Name, DefaultValue);
61 Result := DefaultValue;
62 end;
63end;
64
65function TRegistryEx.ReadStringWithDefault(const Name: string;
66 DefaultValue: string): string;
67begin
68 if ValueExists(Name) then Result := ReadString(Name)
69 else begin
70 WriteString(Name, DefaultValue);
71 Result := DefaultValue;
72 end;
73end;
74
75function TRegistryEx.ReadFloatWithDefault(const Name: string;
76 DefaultValue: Double): Double;
77begin
78 if ValueExists(Name) then Result := ReadFloat(Name)
79 else begin
80 WriteFloat(Name, DefaultValue);
81 Result := DefaultValue;
82 end;
83end;
84
85function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
86var
87 SubKeys: TStringList;
88 I: Integer;
89begin
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;
101end;
102
103function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
104begin
105 {$IFDEF Linux}
106 CloseKey;
107 {$ENDIF}
108 Result := inherited OpenKey(Key, CanCreate);
109end;
110
111function TRegistryEx.GetContext: TRegistryContext;
112begin
113 Result.Key := CurrentPath;
114 Result.RootKey := RootKey;
115end;
116
117procedure TRegistryEx.SetContext(AValue: TRegistryContext);
118begin
119 RootKey := AValue.RootKey;
120 OpenKey(AValue.Key, True);
121end;
122
123function TRegistryEx.ReadBoolWithDefault(const Name: string;
124 DefaultValue: Boolean): Boolean;
125begin
126 if ValueExists(Name) then Result := ReadBool(Name)
127 else begin
128 WriteBool(Name, DefaultValue);
129 Result := DefaultValue;
130 end;
131end;
132
133end.
Note: See TracBrowser for help on using the repository browser.