
Where is the registry in .NET?
Back in January, I
posted about retrieving system Time Zone Information. Without going into a verbose explanation of why, I decided to move the application that utilizes this code to
.NET. In the process of moving this application ‘
forward’ (Is moving to
.NET considered going forward?) I got the impression that the registry might have lost some importance in
.NET. What is the basis for my initial feeling? Well, first take a guess at the namespace where Registry access resides…. If you guessed
Microsoft.Win32 you guess right.
With that being said, and not dwelling on the fact that they places registry access into the Win32 namespace (don't forget to put that in your uses clause) I went ahead and created a new WinForm application with a
System.Windows.Forms.ComboBox (cbxTimeZone) and
System.Windows.Forms.TextBox (txtOutput). I changed the
Multiline property of the TextBox to True and set it to
ReadOnly.
I changed up the code as follows:
const
basetzikey = 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\';
procedure TWinForm.FillComboBox(cbxCombo: ComboBox);
var
rootkey: RegistryKey; //Microsoft.Win32.RegistryKey
subkeynames: array of string;
subkeyname: string;
begin
cbxCombo.Items.Clear;
rootkey:= Registry.LocalMachine;
subkeynames:= rootkey.OpenSubKey(basetzikey,false).GetSubKeyNames;
for subkeyname in subkeynames do
cbxCombo.Items.Add(subkeyname.ToString);
end;
procedure TWinForm.FillTextEdit(timezone: string; txtEdit: TextBox);
type
bytearray= array[] of byte;
var
sb: System.Text.StringBuilder;
rootkey: RegistryKey; //Microsoft.Win32.RegistryKey
subkeyvalues: array of string;
subkeyvalue: string;
keyname: string;
b: bytearray;
i: Smallint;
begin
txtEdit.Clear;
sb:= StringBuilder.Create;
sb.Append(basetzikey);
sb.Append(timezone);
keyname:= sb.ToString;
sb.Remove(0, sb.Length);
rootkey:= Registry.LocalMachine;
subkeyvalues:= rootkey.OpenSubKey(keyname,false).GetValueNames;
for subkeyvalue in subkeyvalues do
begin
sb.Remove(0, sb.Length);
sb.Append(subkeyvalue);
sb.Append(': ');
sb.Append(rootkey.OpenSubKey(keyname,false).GetValue(subkeyvalue).ToString);
sb.Append(#13#10);
if subkeyvalue = 'TZI' then
begin
// you could reporduce the TIME_ZONE_INFORMATION record
// I am just looking for the BIAS which is the first two bytes
// of the structure
b:= bytearray(rootkey.OpenSubKey(keyname,false).GetValue(subkeyvalue));// as bytearray;
i:= b[1] shl 8;
i:= i + b[0];
sb.Append('Bias: ' );
sb.Append(i);
sb.Append(' minutes.');
sb.Append(#13#10);
end;
txtedit.AppendText(sb.ToString);
end;
end;
procedure TWinForm.cbxTimeZone_SelectedIndexChanged(sender: System.Object; e: System.EventArgs);
begin
FillTextEdit(ComboBox(sender).Text,txtOutput);
end;
constructor TWinForm.Create;
begin
inherited Create;
InitializeComponent;
FillComboBox(cbxTimeZone);
end;

The next thing I am going to experiment with is whether or not it is more efficient to clear out and reuse a stringbuilder or create a new one each time I loop through and build the strings as above.
Labels: .NET, Code, Delphi