Posted By

mafro on 04/20/07


Tagged

registry


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

D4RKCODE


Registry


 / Published in: VB.NET
 

Simple registry get/set for Application data.

  1. Imports Microsoft.Win32
  2.  
  3. #Region "Registry"
  4.  
  5. Public Shared Function GetRegKey(ByVal name As String) As String
  6. Dim reg As RegistryKey = Nothing
  7.  
  8. 'regkey is built from CompanyName\ProductName\MajorVersion.MinorVersion
  9. Dim version As String = Convert.ToString(My.Application.Info.Version.Major) & "." & _
  10. Convert.ToString(My.Application.Info.Version.Minor)
  11.  
  12. Try
  13. reg = Registry.CurrentUser.OpenSubKey("Software\" & _
  14. My.Application.Info.CompanyName & "\" & My.Application.Info.ProductName & "\" & version)
  15.  
  16. If reg IsNot Nothing Then
  17. Return Convert.ToString(reg.GetValue(name))
  18. End If
  19. Finally
  20. If reg IsNot Nothing Then reg.Close()
  21. End Try
  22.  
  23. Return Nothing
  24. End Function
  25.  
  26. Public Shared Function SetRegKey(ByVal name As String, ByVal value As String) As Boolean
  27. Dim reg As RegistryKey = Nothing
  28.  
  29. 'regkey is built from CompanyName\ProductName\MajorVersion.MinorVersion
  30. Dim version As String = Convert.ToString(My.Application.Info.Version.Major) & "." & _
  31. Convert.ToString(My.Application.Info.Version.Minor)
  32.  
  33. Try
  34. reg = Registry.CurrentUser.CreateSubKey("Software\" & _
  35. My.Application.Info.CompanyName & "\" & My.Application.Info.ProductName & "\" & version)
  36. reg.SetValue(name, value)
  37. Return True
  38. Catch ex As Exception
  39. Return False
  40. Finally
  41. If reg IsNot Nothing Then reg.Close()
  42. End Try
  43. End Function
  44.  
  45. Public Shared Function DeleteRegKey(ByVal name As String) As Boolean
  46. Dim reg As RegistryKey = Nothing
  47.  
  48. 'regkey is built from CompanyName\ProductName\MajorVersion.MinorVersion
  49. Dim version As String = Convert.ToString(My.Application.Info.Version.Major) & "." & _
  50. Convert.ToString(My.Application.Info.Version.Minor)
  51.  
  52. Try
  53. Registry.CurrentUser.DeleteSubKey("Software\" & _
  54. My.Application.Info.CompanyName & "\" & My.Application.Info.ProductName & "\" & version & "\" & name, False)
  55. Return True
  56. Catch aex As ArgumentException
  57. Return True
  58. Catch ex As Exception
  59. Return False
  60. Finally
  61. If reg IsNot Nothing Then reg.Close()
  62. End Try
  63. End Function
  64.  
  65. Public Shared Function DeleteRegKeyAll() As Boolean
  66. Dim reg As RegistryKey = Nothing
  67.  
  68. 'regkey is built from CompanyName\ProductName\MajorVersion.MinorVersion
  69. Dim version As String = Convert.ToString(My.Application.Info.Version.Major) & "." & _
  70. Convert.ToString(My.Application.Info.Version.Minor)
  71.  
  72. Try
  73. Registry.CurrentUser.DeleteSubKeyTree("Software\" & _
  74. My.Application.Info.CompanyName & "\" & My.Application.Info.ProductName & "\" & version)
  75. Return True
  76. Catch aex As ArgumentException
  77. 'key doesnt exist
  78. Return True
  79. Catch ex As Exception
  80. Return False
  81. Finally
  82. If reg IsNot Nothing Then reg.Close()
  83. End Try
  84. End Function
  85.  
  86. #End Region

Report this snippet  

You need to login to post a comment.