在VB.NET中如何快速访问注册表的技巧 代码
<p>vb.net中访问注册表变得非常的简单。我们可以用microsoft.Win32 名称空间的下的registry类和registryKey类。另外My.Computer.Registry 也可以返回一个Microsoft.Win32.Registry类的实例。 <br /> 下面就举几个小例子来说明vb.net访问注册表的方法。 <br /> 1、返回或创建一个注册表键</p><table border="1" cellspacing="0" bordercolorlight="#000000" bordercolordark="#ffffff" cellpadding="2" width="400" align="center">
<tbody>
<tr>
<td class="code" bgcolor="#e6e6e6">
<pre><p> Dim Key1 As Microsoft.Win32.RegistryKey <br />Key1 = My.Computer.Registry.CurrentUser '返回当前用户键 <br />Dim Key2 As Microsoft.Win32.RegistryKey <br />Key2 = Key1.OpenSubKey("northsnow") '返回当前用户键下的northsnow键 <br />If Key2 Is Nothing Then <br />Key2 = Key1.CreateSubKey("northsnow") '如果键不存在就创建它 <br />End If </p></pre>
</td>
</tr>
</tbody>
</table>
<p> 2、删除注册表键 </p>
<table border="1" cellspacing="0" bordercolorlight="#000000" bordercolordark="#ffffff" cellpadding="2" width="400" align="center">
<tbody>
<tr>
<td class="code" bgcolor="#e6e6e6">
<pre><p> Dim Key1 As Microsoft.Win32.RegistryKey <br />Key1 = My.Computer.Registry.CurrentUser '返回当前用户键 <br />Dim Key2 As Microsoft.Win32.RegistryKey <br />Key2 = Key1.OpenSubKey("northsnow") '返回当前用户键下的northsnow键 <br />If Not Key2 Is Nothing Then <br />Key1.DeleteSubKey("northsnow") '如果键不存在就创建它 <br />End If </p></pre>
</td>
</tr>
</tbody>
</table>
<p> 3、创建或读取注册表项 </p>
<table border="1" cellspacing="0" bordercolorlight="#000000" bordercolordark="#ffffff" cellpadding="2" width="400" align="center">
<tbody>
<tr>
<td class="code" bgcolor="#e6e6e6">
<pre><p> Dim Key1 As Microsoft.Win32.RegistryKey <br />Key1 = My.Computer.Registry.CurrentUser '返回当前用户键 <br />Dim Key2 As Microsoft.Win32.RegistryKey <br />Key2 = Key1.OpenSubKey("northsnow", True) '返回当前用户键下的northsnow键,</p><p> 如果想创建项,必须指定第二个参数为true <br />If Key2 Is Nothing Then <br />Key2 = Key1.CreateSubKey("northsnow") '如果键不存在就创建它 <br />End If <br />'创建项,如果不存在就创建,如果存在则覆盖 <br />Key2.SetValue("name", "塞北的雪") <br />Key2.SetValue("sex", True) <br />Key2.SetValue("age", 30) <br />'返回项值 <br />Dim sb As New System.Text.StringBuilder <br />sb.AppendLine(Key2.GetValue("name")) <br />sb.AppendLine(Key2.GetValue("sex")) <br />sb.AppendLine(Key2.GetValue("age")) <br />MsgBox(sb.ToString) <br />'查验某个项是否存在 <br />If (Key2.GetValue("name")) Is Nothing Then <br />MsgBox("no") <br />Else <br />MsgBox("yes") <br />End If <br />If (Key2.GetValue("name2")) Is Nothing Then <br />MsgBox("no") <br />Else <br />MsgBox("yes") <br />End If </p><p> '输出 <br />' 塞北的雪 <br />'True <br />'30 <br />'yes <br />'no </p></pre>
</td>
</tr>
</tbody>
</table>
<br />4、遍历注册表 <br /> 这个也非常简单, 在窗体上放一个按钮和两个文本框,添加如下的代码:
<table border="1" cellspacing="0" bordercolorlight="#000000" bordercolordark="#ffffff" cellpadding="2" width="400" align="center">
<tbody>
<tr>
<td class="code" bgcolor="#e6e6e6">
<pre><p> Dim sb As New System.Text.StringBuilder '返回遍历结果 <br />Dim sb2 As New System.Text.StringBuilder '返回读取出错的注册表键 <br />Private Sub Button3_Click()Sub Button3_Click(ByVal sender As System.Object, </p><p> ByVal e As System.EventArgs) Handles Button3.Click <br />Dim Key1 As Microsoft.Win32.RegistryKey <br />Key1 = My.Computer.Registry.CurrentUser '返回当前用户键 <br />If Not Key1 Is Nothing Then <br />sb.AppendLine(Key1.Name) <br />readValue(Key1) <br />readReg(Key1) <br />End If <br />Me.TextBox1.Text = sb.ToString <br />Me.TextBox2.Text = sb2.ToString <br />End Sub <br />'遍历注册表键树 <br />Private Sub readReg()Sub readReg(ByVal r As Microsoft.Win32.RegistryKey) <br />If r.SubKeyCount > 0 Then <br />Dim keyName() As String <br />Dim keyTemp As Microsoft.Win32.RegistryKey <br />keyName = r.GetSubKeyNames <br />Dim i As Integer <br />For i = 0 To keyName.GetLength(0) - 1 <br />Try <br />sb.AppendLine(keyName(i)) <br />keyTemp = r.OpenSubKey(keyName(i), True) <br />readValue(keyTemp) <br />readReg(keyTemp) <br />Catch ex As Exception <br />sb2.AppendLine(keyName(i)) <br />End Try <br />Next <br />End If <br />End Sub <br />'遍历某键下的项 <br />Private Sub readValue()Sub readValue(ByVal r As Microsoft.Win32.RegistryKey) <br />If r.ValueCount > 0 Then <br />Dim valueName() As String <br />Dim i As Integer <br />valueName = r.GetValueNames <br />For i = 0 To valueName.GetLength(0) - 1 <br />sb.AppendLine("####") <br />sb.Append(r.Name) <br />sb.Append("----") <br />sb.Append(r.GetValue(valueName(i)).ToString) <br />Next <br />End If <br />End Sub</p></pre>
</td>
</tr>
</tbody>
</table>
<p> </p>
頁:
[1]