(MVC) MVC (2014 год)

Простий сервіс реестра для зберігання вводу користувача десктопних прог.

Нище ви бачите мій найпростіший, але дуже зручний, врапер для зберігання вводу користувача десткопних прог у реєстрі, яким я користуюсь вже багато років.



   1:  Public Class Registry
   2:   
   3:      Public Event FirstStart()
   4:      Public Event Err(Msg As String)
   5:      Public ReadOnly Property SoftwareName As String 'LawInfo
   6:   
   7:      Public Sub New(SoftwareName1 As String)
   8:          SoftwareName = SoftwareName1
   9:          Try
  10:              Using CurrentKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\" & SoftwareName, True)
  11:                  If CurrentKey Is Nothing Then
  12:                      Using SOFTWARE As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE", True)
  13:                          Using MyKey As Microsoft.Win32.RegistryKey = SOFTWARE.CreateSubKey(SoftwareName)
  14:                              RaiseEvent FirstStart()
  15:                          End Using
  16:                      End Using
  17:                  End If
  18:              End Using
  19:          Catch ex As Exception
  20:              RaiseEvent Err(ex.Message)
  21:          End Try
  22:      End Sub
  23:   
  24:      Public Sub SetValue(Name As String, Value As Object, RegistryValueKind As Microsoft.Win32.RegistryValueKind)
  25:          Using CurrentKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\" & SoftwareName, True)
  26:              If CurrentKey IsNot Nothing Then
  27:                  CurrentKey.SetValue(Name, Value, RegistryValueKind)
  28:              Else
  29:                  RaiseEvent Err("RegistryKey SOFTWARE\" & SoftwareName & " missing")
  30:              End If
  31:          End Using
  32:      End Sub
  33:   
  34:      Public Sub SetValue(Name As String, Value As String)
  35:          Using CurrentKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\" & SoftwareName, True)
  36:              If CurrentKey IsNot Nothing Then
  37:                  CurrentKey.SetValue(Name, Value)
  38:              Else
  39:                  RaiseEvent Err("RegistryKey SOFTWARE\" & SoftwareName & " missing")
  40:              End If
  41:          End Using
  42:      End Sub
  43:   
  44:      Public Sub SetValue(Name As String, Value As Byte())
  45:          Using CurrentKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\" & SoftwareName, True)
  46:              If CurrentKey IsNot Nothing Then
  47:                  CurrentKey.SetValue(Name, Value, Microsoft.Win32.RegistryValueKind.Binary)
  48:              Else
  49:                  RaiseEvent Err("RegistryKey SOFTWARE\" & SoftwareName & " missing")
  50:              End If
  51:          End Using
  52:      End Sub
  53:   
  54:      Public Sub SetValue(Name As String, Value As UInteger)
  55:          Using CurrentKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\" & SoftwareName, True)
  56:              If CurrentKey IsNot Nothing Then
  57:                  CurrentKey.SetValue(Name, Value, Microsoft.Win32.RegistryValueKind.DWord)
  58:              Else
  59:                  RaiseEvent Err("RegistryKey SOFTWARE\" & SoftwareName & " missing")
  60:              End If
  61:          End Using
  62:      End Sub
  63:   
  64:      Public Sub SetValue(Name As String, Value As ULong)
  65:          Using CurrentKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\" & SoftwareName, True)
  66:              If CurrentKey IsNot Nothing Then
  67:                  CurrentKey.SetValue(Name, Value, Microsoft.Win32.RegistryValueKind.QWord)
  68:              Else
  69:                  RaiseEvent Err("RegistryKey SOFTWARE\" & SoftwareName & " missing")
  70:              End If
  71:          End Using
  72:      End Sub
  73:   
  74:      Public Sub SetValue(Name As String, Value As Date)
  75:          Using CurrentKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\" & SoftwareName, True)
  76:              If CurrentKey IsNot Nothing Then
  77:                  CurrentKey.SetValue(Name, Value.ToString, Microsoft.Win32.RegistryValueKind.String)
  78:              Else
  79:                  RaiseEvent Err("RegistryKey SOFTWARE\" & SoftwareName & " missing")
  80:              End If
  81:          End Using
  82:      End Sub
  83:   
  84:      Public Sub SetValue(Name As String, Value As Boolean)
  85:          Using CurrentKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\" & SoftwareName, True)
  86:              If CurrentKey IsNot Nothing Then
  87:                  CurrentKey.SetValue(Name, Value.ToString, Microsoft.Win32.RegistryValueKind.String)
  88:              Else
  89:                  RaiseEvent Err("RegistryKey SOFTWARE\" & SoftwareName & " missing")
  90:              End If
  91:          End Using
  92:      End Sub
  93:   
  94:      Public Sub SetValue(Name As String, Value As Decimal)
  95:          Using CurrentKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\" & SoftwareName, True)
  96:              If CurrentKey IsNot Nothing Then
  97:                  CurrentKey.SetValue(Name, Value.ToString, Microsoft.Win32.RegistryValueKind.String)
  98:              Else
  99:                  RaiseEvent Err("RegistryKey SOFTWARE\" & SoftwareName & " missing")
 100:              End If
 101:          End Using
 102:      End Sub
 103:   
 104:      Public Function GetValue(Of T)(Name As String) As Object
 105:          Using CurrentKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\" & SoftwareName, True)
 106:              If CurrentKey IsNot Nothing Then
 107:                  Dim ValueType As Microsoft.Win32.RegistryValueKind
 108:                  Dim Ret As Object = CurrentKey.GetValue(Name, Nothing, ValueType)
 109:                  If Ret IsNot Nothing Then
 110:                      If GetType(T) = GetType(String) Then
 111:                          Return Ret.ToString
 112:                      ElseIf GetType(T) = GetType(Decimal) Then
 113:                          Return Decimal.Parse(Ret.ToString)
 114:                      ElseIf GetType(T) = GetType(Boolean) Then
 115:                          Return Boolean.Parse(Ret.ToString)
 116:                      ElseIf GetType(T) = GetType(Date) Then
 117:                          Return Date.Parse(Ret.ToString)
 118:                      ElseIf GetType(T) = GetType(Byte()) Then
 119:                          Return TryCast(Ret, Byte())
 120:                      ElseIf GetType(T) = GetType(UInteger) Then
 121:                          Return DirectCast(Ret, UInteger)
 122:                      ElseIf GetType(T) = GetType(ULong) Then
 123:                          Return DirectCast(Ret, ULong)
 124:                      Else
 125:                          Return Ret
 126:                      End If
 127:                  End If
 128:              End If
 129:          End Using
 130:      End Function
 131:   
 132:  End Class

Користуватися ним так - коли завантажується перша форма проєкта, то усі текстбокси відновлюються з реестру, наприклад ось так.



А закусуються/защелкуються дані у реестр - коли та чі інша операція успішно закінчується.







Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19
Link to this page: http://www.vb-net.com/Registry/index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <MAIL ME>  <ABOUT ME>  < THANKS ME>