VB.Net Tutorial/Windows/Registry

Материал из VB Эксперт
Перейти к: навигация, поиск

CPU information from Registry

Imports System
Imports Microsoft.win32
Imports System.Diagnostics
Imports System.Windows.Forms
 
 
 Public Class Tester
    Public Shared Sub Main
    
        Dim m_LM As RegistryKey
        Dim m_HW As RegistryKey
        Dim m_Des As RegistryKey
        Dim m_System As RegistryKey
        Dim m_CPU As RegistryKey
        Dim m_Info As RegistryKey
        m_LM = Registry.LocalMachine
        m_HW = m_LM.OpenSubKey("HARDWARE")
        m_Des = m_HW.OpenSubKey("DESCRIPTION")
        m_System = m_Des.OpenSubKey("SYSTEM")
        m_CPU = m_System.OpenSubKey("CentralProcessor")
        m_Info = m_CPU.OpenSubKey("0")
 
        Console.WriteLine(m_Info.GetValue("VendorIdentifier"))
        Console.WriteLine(m_Info.GetValue("ProcessorNameString"))
        Console.WriteLine(m_Info.GetValue("Identifier"))
        Console.WriteLine(m_Info.GetValue("~Mhz") & "MHz")
    End Sub
End Class
AuthenticAMD
Mobile AMD Sempron(tm) Processor 3000+
x86 Family 15 Model 28 Stepping 0
1790MHz

Get string value from Registry

Option Strict On
Imports Microsoft.Win32
Public Module GetRegistryValue
   Public Sub Main()
      Dim appTextFiles As String = _
              CStr(Registry.GetValue("HKEY_CLASSES_ROOT\txtfile\shell\open\command", _
              "", String.Empty))
      appTextFiles = Left(appTextFiles, InStr(1, appTextFiles, "") - 1)
      Console.WriteLine("Text files are handled by {0}.", appTextFiles.Trim())
   End Sub
End Module
Text files are handled by C:\WINDOWS\system32\NOTEPAD.EXE.

Get value from Registry with default value

public class Test
   public Shared Sub Main
        Console.WriteLine(My.ruputer.Registry.GetValue("HKEY_CURRENT_USER\Software\yourname\subname\","PromptOnExit", "0"))
   End Sub
End Class

My.Computer.Registry.GetValue

Option Strict On
Public Module RegistryTest
   Public Sub Main()
      Dim data As Object
      data = My.ruputer.Registry.GetValue("HKEY_CLASSES_ROOT\wrifile\DefaultIcon", _
                                            String.Empty, String.Empty)
      If TypeOf data Is String Then
         Console.WriteLine("String data: " & data.ToString())
      ElseIf TypeOf data Is Integer Then
         Console.WriteLine("Integer data: " & CInt(data))
      ElseIf TypeOf data Is String() Then
         Console.WriteLine("String array: ")
         Dim dataArray() As String = DirectCast(data, String())
         For ctr As Integer = LBound(dataArray, 1) To UBound(dataArray, 1)
            Console.WriteLine(" " & dataArray(ctr))
         Next
      ElseIf typeOf data Is Byte() Then
         Console.WriteLine("Binary data: ")
         Dim byteArray() As Byte = DirectCast(data, Byte())
         For ctr As Integer = LBound(byteArray, 1) To UBound(byteArray, 1)
            Console.Write(" " & Hex(byteArray(ctr)) & " ")
         Next
      Else
         Console.WriteLine("Unknown data type...")
      End If
   End Sub
End Module
String data: "C:\Program Files\Windows NT\Accessories\WORDPAD.EXE",2

Save value to Registry: RegistryValueKind.DWord, RegistryValueKind.String, RegistryValueKind.Binary, RegistryValueKind.MultiString

Option Strict On
Imports Microsoft.Win32
Public Module Tester
   Public Sub Main
      Dim regKey As RegistryKey
      Dim keyTop As RegistryKey = Registry.CurrentUser
      regKey = keyTop.OpenSubkey("Software\MyCompany\MyApp", True)
      If regKey Is Nothing Then
         regKey = keyTop.CreateSubKey("Software\MyCompany\MyApp")
      End If
      Dim binValue As Byte() = {&HF0, &HFF, &H12, &HE0, &H43, &HAC}
      Dim strngValue As String() = {"A", "B","C", "D"}
      "regKey.SetValue("WindowState", 0, RegistryValueKind.DWord)
      "regKey.SetValue("CustomWindowCaption", "Client Contact Management",RegistryValueKind.String)
      "regKey.SetValue("CustomPosition", binValue, RegistryValueKind.Binary)
      "regKey.SetValue("CustomLabels", strngValue, RegistryValueKind.MultiString)
   End Sub
End Module

Set Registry value

Option Strict On
Imports Microsoft.Win32
Public Module RegistrySetValue
   Public Sub Main
      Dim regData As Byte() = {&HF0, &HFF, &H20, &H00}
      Dim regTopKey As String = "HKEY_LOCAL_MACHINE"
      Dim regPath As String = "\Software\MyCompany\MyApp"
"      Registry.SetValue(regTopKey & regPath, "BinData", regData, RegistryValueKind.Binary)
   End Sub
End Module

Set value from Registry using My.Computer.Registry

public class Test
   public Shared Sub Main
        "My.ruputer.Registry.SetValue("HKEY_CURRENT_USER\Software\name\subname\", "UserName", "new name")
   End Sub
End Class