LboxReg

  VBによるクラス作成



LboxRegClass は、HKEY_CLASSES_ROOT のみ対象です
GetAll では、連想配列 に KEY と KEY の値のペアをセットします
GetAllValue では、連想配列に KEYの値の名前 と 値のペアをセットします
他のルートキーの対応

アセンブリ名 : lightbox.tool
ルート名前空間 : lightbox.tool



  
Imports Microsoft.Win32
Imports System.Collections.Generic

Public Class LboxRegClass

	Public subkey As String
	' このプロパティを使用して、Framework の通常のレジストリ処理を実行する
	Public regkey As RegistryKey

	' ****************************************
	' デフォルトコンストラクタ
	' ****************************************
	Public Sub New()
		MyBase.New()
	End Sub

	' ****************************************
	' コンストラクタ1
	' ****************************************
	Public Sub New(ByVal key As String)
		MyBase.New()

		Me.subkey = key
		Me.regkey = Registry.ClassesRoot.OpenSubKey(Me.subkey, True)

	End Sub

	' ****************************************
	' コンストラクタで指定したパス以下にある
	' 全ての KEY とデフォルトの値を
	' 文字列:文字列の連想配列にして返す
	' ****************************************
	Public Function GetAll() As Dictionary(Of String, String)

		Dim dic As New Dictionary(Of String, String)

		If regkey Is Nothing Then
			GetAll = Nothing
			Exit Function
		End If

		Dim keyNames As String() = regkey.GetSubKeyNames()

		Dim str As String
		For Each str In keyNames
			dic.Add(str, "")
		Next

		Dim subkey As RegistryKey

		For Each str In keyNames
			subkey = Me.regkey.OpenSubKey(str, False)
			dic(str) = CType(subkey.GetValue(""), String)
			subkey.Close()
		Next

		GetAll = dic

	End Function

	' ****************************************
	' コンストラクタで指定したパス内の
	' 全ての エントリと値を
	' 文字列:文字列の 連想配列にして返す
	' "" ( 空文字列 ) に対してはデフォルトの値
	' ****************************************
	Public Function GetAllValue() As Dictionary(Of String, String)

		Dim dic As New Dictionary(Of String, String)

		If regkey Is Nothing Then
			GetAllValue = Nothing
			Exit Function
		End If

		Dim valueNames As String() = regkey.GetValueNames()

		Dim str As String
		For Each str In valueNames
			dic.Add(str, "")
		Next

		Dim type As RegistryValueKind
		Dim bytes As Byte()
		For Each str In valueNames
			type = regkey.GetValueKind(str)
			' 通常文字列
			If type = RegistryValueKind.String Then
				dic(str) = CType(regkey.GetValue(str), String)
			End If
			' 環境変数を使用する文字列
			If type = RegistryValueKind.ExpandString Then
				dic(str) = CType(regkey.GetValue(str), String)
			End If
			' DWORD
			If type = RegistryValueKind.DWord Then
				dic(str) = CType(regkey.GetValue(str), String)
			End If
			' バイナリ
			If type = RegistryValueKind.Binary Then
				bytes = CType(regkey.GetValue(str), Byte())
				Dim work As String = ""
				For I As Integer = 0 To bytes.Length - 1
					If I <> 0 Then
						work &= " "
					End If
					work &= bytes(I).ToString()
				Next
				dic(str) = work
			End If
		Next

		GetAllValue = dic

	End Function

	' ****************************************
	' 閉じる
	' ****************************************
	Sub Close()

		If regkey Is Nothing Then
			Exit Sub
		End If

		Me.regkey.Close()

	End Sub

End Class
  



  利用サンプルコード



  
Dim reg As New LboxRegClass("CLSID\{450D8FBA-AD25-11D0-98A8-0800361B1103}\shell")
Dim dic As Dictionary(Of String, String) = reg.GetAll()
reg.close()
  




  
Me.LboxGrid1.AddColumn("KEY", "キー")
Me.LboxGrid1.AddColumn("VALUE", "タイトル")
Me.LboxGrid1.AddColumn("PATH", "実行パス")

' アクセス対象キー
Dim regpath As String = "CLSID\{450D8FBA-AD25-11D0-98A8-0800361B1103}\shell"
' インスタンス作成
Dim reg As New LboxRegClass(regpath)
' 対症の下にある全てのキー文字列と値を取得
Dim dic As Dictionary(Of String, String) = reg.GetAll()
' インスタンス使用終了
reg.Close()

' 連想配列の列挙
For Each kvp As KeyValuePair(Of String, String) In dic

	' 行追加
	Me.LboxGrid1.AddRow()
	' キー文字列セット
	Me.LboxGrid1.SetColumnText("KEY", kvp.Key)
	' キーの値セット
	Me.LboxGrid1.SetColumnText("VALUE", kvp.Value)

	' キーの下にある "command" キーのインスタンス作成
	Dim regvalue As New LboxRegClass(regpath & "\" & kvp.Key & "\command")
	' 値の一覧を取得
	Dim dic2 As Dictionary(Of String, String) = regvalue.GetAllValue()
	' インスタンス使用終了
	regvalue.Close()

	' "command" キーの値をセット
	Me.LboxGrid1.SetColumnText("PATH", dic2(""))

Next kvp
  



  他のルートキーの対応

  
Public Class LboxRegCurrent
	Inherits LboxRegClass

	' ****************************************
	' コンストラクタ
	' ****************************************
	Public Sub New(ByVal key As String)
		MyBase.New()

		Me.subkey = key
		Me.regkey = Registry.CurrentUser.OpenSubKey(Me.subkey, True)

	End Sub

End Class

Public Class LboxRegLocal
	Inherits LboxRegClass

	' ****************************************
	' コンストラクタ
	' ****************************************
	Public Sub New(ByVal key As String)
		MyBase.New()

		Me.subkey = key
		Me.regkey = Registry.LocalMachine.OpenSubKey(Me.subkey, True)

	End Sub

End Class
  



  更新サンプル

  
If MessageBox.Show( _
 "更新してもよろしいですか?", _
 "確認", _
 MessageBoxButtons.YesNo, _
 MessageBoxIcon.Question, _
 MessageBoxDefaultButton.Button2) = System.Windows.Forms.DialogResult.No Then
	Exit Sub
End If

' アクセスする「マイ ドキュメント」のキー
Dim regpath As String = "CLSID\{450D8FBA-AD25-11D0-98A8-0800361B1103}\shell"
Dim reg As LboxRegClass
Dim regChild As Microsoft.Win32.RegistryKey

Dim target As String
Dim targetValue As String
Dim commandValue As String

Me.LboxGrid1.nCurrentRow = -1
Do While (Me.LboxGrid1.FindNextRow())

	target = Me.LboxGrid1.GetColumnText("KEY")
	targetValue = Me.LboxGrid1.GetColumnText("VALUE")
	commandValue = Me.LboxGrid1.GetColumnText("PATH")

	' LboxRegClass のインスタンス作成
	reg = New LboxRegClass(regpath & "\" & target)
	' そのキーへのアクセスは、regkey プロパティを使用
	' "" で、キーに対する値( 規定 ) にアクセス
	reg.regkey.SetValue("", targetValue)

	' 下階層は、regkey プロパティ からさらにキーを Open する
	regChild = reg.regkey.OpenSubKey("command", True)
	regChild.SetValue("", commandValue)
	regChild.Close()

	reg.Close()

Loop
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ