| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 注册表编程 >> 实例分析正文
最新推荐 更多内容
[组图]注册表结构分析与vb.net编程的结合
注册表结构分析与vb.net编程的结合
作者:Manoj G    来源:Codeproject     更新时间:2009-9-1

简介

开发者通常喜欢把应用程序的特别信息或者配置信息放到注册表中,注册表被用来保存一些例如数据库连接语句,配置文件等等,保存在注册表中会比保存在文件中读取更快而且放在注册表这个系统安全级的文件中将更可靠,ini配置文件有他自己的局限性。本文乐博网翻译了部分 

本文探讨了一些基本的注册表常识,解释vb.net如何进行基本的注册表操作

Basics of Windows Registry

The registry is organized as a hierarchical structure. It has basically five predefined keys under which all data is added or accessed. These keys cannot be renamed or deleted .Given below is a table containing a brief description about them.

Subtree

Definition

HKEY_CURRENT_USER

This contains configuration information of a user who is currently logged on to the system.That is, user profile data is stored here

HKEY_USERS

Contains all user profiles on the computer. HKEY_CURRENT_USER is actually an alias for a key in the HKEY_USERS sub tree.

HKEY_LOCAL_MACHINE

Contains configuration information particular to the computer, irrespective of which user is logged on.

HKEY_CLASSES_ROOT

Contains data that associates file types with programs, and configuration data for COM objects.

HKEY_CURRENT_CONFIG

Contains information about the hardware profile used by the local computer at system startup.

Each key has many subkeys and may have a value. Given below is a snapshot of the registry as seen through the registry editor (Regedit.exe), which comes along with windows.

 

Fig 1: 注册表结构

In the snapshot shown above, each node under My Computer is a key. For example , HKEY_CURRENT_CONFIG is a key which has two subkeys: Software and System. Fonts is a subkey under software and has values. Each Value contains a name and its associated data. Each value needs to be associated with a particular data type. Given below is a table containing the important data types

Data types

Used for

REG_SZ

A fixed-length text string. Boolean (True or False) values and other short text values usually have this data type.

REG_EXPAND_SZ

A variable-length text string that can include variables that are resolved when an application or service uses the data.

REG_DWORD

Data represented by a number that is 4 bytes (32 bits) long.

REG_MULTI_SZ

Multiple text strings formatted as an array of null-terminated strings, and terminated by two null characters.

Referring back to figure 1, there is a value called LogPixels which has data of type REG_DWORD and value 96.

用 Microsoft.Win32 名空间操作注册表

The operations on the registry in .NET can be done using two classes of the Microsoft.Win32 Namespace: Registry class and the RegistryKey class.The Registry class provides base registry keys as shared public (read-only) methods: 

ClassesRoot

This field reads the Windows registry base key HKEY_CLASSES_ROOT.

CurrentConfig

Reads the Windows registry base key HKEY_CURRENT_CONFIG.

CurrentUser

Reads the Windows registry base key HKEY_CURRENT_USER

LocalMachine

This field reads the Windows registry base key HKEY_LOCAL_MACHINE.

Users

This field reads the Windows registry base key HKEY_USERS.

Each of the public methods shown above provides an object of the RegistryKey class whose methods can be used to access subkeys under the corresponding keys. The important members of the RegistryKey class are enlisted below

公共属性

Name

Retrieves the name of the key.

SubKeyCount

Retrieves the count of subkeys at the base level, for the current key.

ValueCount

Retrieves the count of values in the key.

Public Methods

Close

Closes the key and flushes it to disk if the contents have been modified.

CreateSubKey

Creates a new subkey or opens an existing subkey.

DeleteSubKey

Deletes the specified subkey.

DeleteSubKeyTree

Deletes a subkey and any child subkeys recursively.

DeleteValue

Deletes the specified value from this key.

Flush

Writes all the attributes of the specified open registry key into the registry.

GetSubKeyNames

Retrieves an array of strings that contains all the subkey names.

GetValue

Retrieves the specified value.

GetValueNames

Retrieves an array of strings that contains all the value names associated with this key.

OpenSubKey

Retrieves a specified subkey, with the write access as specified.

SetValue

Sets the specified value.

VB.NET and Registry

I shall now come to the practical aspect and see how the registry can be manipulated using VB.NET code. I have considered three basic operations: Creating a subkey, adding values and deleting a subkey

创建一个子键
Collapse Copy Code
Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
regKey.CreateSubKey("MyApp")
regKey.Close()
        
In the code snippet shown above, I have created a subkey under HKLM\Software called MyApp. Note that I passed True as the second parameter to the OpenSubKey method. This boolean value is to indicate whether the key is writable or not. For instance, you can set it to false if you are just reading data from the registry.
读写注册表键值

Collapse Copy Code
Dim regKey As RegistryKey
Dim ver As Decimal
regKey = Registry.LocalMachine.OpenSubKey("Software\MyApp", True)
regKey.SetValue("AppName", "MyRegApp")
ver = regKey.GetValue("Version", 0.0)
If ver <  1.1 Then
regKey.SetValue("Version", 1.1)
End If
regKey.Close()

In the code snippet shown above, I am creating two values AppName and Version. I am also setting the values to MyRegApp and 1.1 respectively. Note: If you recollect that in the previous sections I had mentioned about data types for registry values like REG_SZ. But nowhere in the above code we mentioned about the data type. This is because .NET runtime interprets the type itself based on what is passed as value and we do not need to pass it explicitly.

删除一个子键

Collapse Copy Code
Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("Software", True)
regKey.DeleteSubKey("MyApp", True)
regKey.Close()
        

In the code snippet shown above, I am deleting the subkey MyApp and all its values. Note that in the call to DeleteSubkey, I have passed a second Boolean argument of True. This means that an exception is thrown when the key to be deleted is not found

Important
  • To read and write to the registry you need security permissions. If you do not have sufficient permissions, then you will get a SecurityException when you try to access or create keys.
  • The registry is a very sensitive part of the Windows Operating system. So, it is imperative that you take a backup of the registry before attempting to play around with it. A corrupt registry could render the operating system non functional.
  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    VB.Net注册表编程的实例[Dream]
    VB.Net注册表中读取值的实例
    VB.Net注册表权限编程的分析[乐博网原创]
    Registry.LocalMachine属性的实例
    RegistryKey.OpenRemoteBaseKey方法的实例
    Registry.Users属性的实例
    staticGetValue和SetValue操作注册表实例
    RegistryKey.OpenSubKey打开注册表键值实例
    VB.Net注册表键值类型的分析[乐博网原创]
    VB.Net从注册表读取Excel版本信息
    VB.Net操作注册表实例
    VB.NET注册表编程实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]