source: trunk/Packages/Common/URegistry.pas

Last change on this file was 18, checked in by chronos, 12 years ago
  • Used external packages are now stored in uncompressed form rather in zipped files. This allow better package version synchronisation.
File size: 2.7 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 public
29 function ReadBoolWithDefault(const Name: string;
30 DefaultValue: Boolean): Boolean;
31 function ReadIntegerWithDefault(const Name: string; DefaultValue: Integer): Integer;
32 function ReadStringWithDefault(const Name: string; DefaultValue: string): string;
33 function ReadFloatWithDefault(const Name: string;
34 DefaultValue: Double): Double;
35 function DeleteKeyRecursive(const Key: string): Boolean;
36 end;
37
38function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
39
40
41implementation
42
43function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
44begin
45 Result.RootKey := RootKey;
46 Result.Key := Key;
47end;
48
49{ TRegistryEx }
50
51function TRegistryEx.ReadIntegerWithDefault(const Name: string;
52 DefaultValue: Integer): Integer;
53begin
54 if ValueExists(Name) then Result := ReadInteger(Name)
55 else begin
56 WriteInteger(Name, DefaultValue);
57 Result := DefaultValue;
58 end;
59end;
60
61function TRegistryEx.ReadStringWithDefault(const Name: string;
62 DefaultValue: string): string;
63begin
64 if ValueExists(Name) then Result := ReadString(Name)
65 else begin
66 WriteString(Name, DefaultValue);
67 Result := DefaultValue;
68 end;
69end;
70
71function TRegistryEx.ReadFloatWithDefault(const Name: string;
72 DefaultValue: Double): Double;
73begin
74 if ValueExists(Name) then Result := ReadFloat(Name)
75 else begin
76 WriteFloat(Name, DefaultValue);
77 Result := DefaultValue;
78 end;
79end;
80
81function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
82var
83 SubKeys: TStringList;
84 I: Integer;
85begin
86 try
87 SubKeys := TStringList.Create;
88 if OpenKey(Key, False) and HasSubKeys then begin
89 GetKeyNames(SubKeys);
90 for I := 0 to SubKeys.Count - 1 do
91 DeleteKeyRecursive(Key + '\' + SubKeys[I]);
92 end;
93 Result := DeleteKey(Key);
94 finally
95 SubKeys.Free;
96 end;
97end;
98
99function TRegistryEx.ReadBoolWithDefault(const Name: string;
100 DefaultValue: Boolean): Boolean;
101begin
102 if ValueExists(Name) then Result := ReadBool(Name)
103 else begin
104 WriteBool(Name, DefaultValue);
105 Result := DefaultValue;
106 end;
107end;
108
109end.
Note: See TracBrowser for help on using the repository browser.