Gone with the wind

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

如何读取Windows注册表信息

在介绍如何读取注册表信息前,先解释如何增加注册条目。


1、增加注册表数据


以下文本是实际的注册信息。将这个内容复制并粘贴到一个文本文件中,将其保存扩展名为.reg的文件,然后双击这个文件进行注册表内容的输入操作。


/*Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\TAW\BSE]

"DSN"="TAWReports"

"User"="TAW1"

"Password"="taw1.1"

"Server"="dbserver"

"IP"=""

*/


2、阅读注册数据


2.1 创建两个RegistryKey变量。

2.2 创建一个内含od对象的类,第一个参数为HKEY(主根关键字名),第二个为"",表示本地机器。

2.3 在要阅读信息的地方创建子关键字。

2.4 用RegistryKey类的Getvalue方法来读取某一特定节点关键字信息的数据。在这里,DSN,Server,Password就是节点。


以下代码读取刚才加入到注册表中的注册数据:


namespace CONAPP

{

using System;

using Microsoft.Win32;


public class Class1

{

public Class1()

{

//

// TODO: Add Constructor Logic here

//

}


public static int Main(string[] args)

{


RegistryKey SUBKEY;

RegistryKey TAWKAY = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.CurrentUser,"");

string subkey = "Software\\TAW\\BSE";

SUBKEY = TAWKAY.OpenSubKey(subkey);

object dsn = SUBKEY.GetValue("DSN");

object user = SUBKEY.GetValue("user");

object password = SUBKEY.GetValue("password");

object server = SUBKEY.GetValue("server");

return 0;

}

}

}


这里是可下载代码包:http://www.mindcracker.com/csharp/1/read_reg.cs

posted on 2004-10-25 00:39  Gone with the wind  阅读(472)  评论(0)    收藏  举报