BPSoftware.com
Home   Utilities   Purchase   FAQ   Support   Contact        
Shareware Utilities
 APrintDirect
 AIconExtract
 AFile Attribute Manager
Freeware Utilities
 AddrMon
 AFileSync
 ASysIcon
 B&P Table Utilities
 BPACLer
 BPSNMPMon
 BPSNMPUtil
 CharCount
 Delphi® Components
 MacAddr
Miscellaneous
 BPSoftware Blog
 Purchase Shareware
 Support

 Subscribe!

Sunday, April 09, 2006
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: , ,

posted by Brad Prendergast at 2:00:00 PM
Comments:
I think of moving code to .Net as a lateral move. Not forward or back. :)
posted by Anonymous Bruce McGee Sunday, April 09, 2006 11:10:00 PM  
Links to this post:

Create a Link

Recent Posts
 Managing my Newsgroups
 Meaningful Information
 Rock Paper Scissors
 HTML Element
 ARP! ARP!
 Rightly So
 Easy Does It
 Syndicate Me
 Around the network of silken thread
 I did not win

 Subscribe!


Labels



Archives
 October 2005
 November 2005
 December 2005
 January 2006
 February 2006
 March 2006
 April 2006
 May 2006
 June 2006
 July 2006
 August 2006
 September 2006
 December 2006
 January 2007
 February 2007
 March 2007
 September 2007
 October 2007
 November 2007
 July 2008
 November 2008
Powered by Blogger