Sub Main()
' 既定のテキスト・エディタのパスを取得する
Dim textEditorPath As String = GetDefaultTextEditorExePath()
' 既定のテキスト・エディタを立ち上げる
Process.Start(textEditorPath)
End Sub
Private Function GetDefaultTextEditorExePath() As String
' 「HKEY_CLASSES_ROOT\.txt」の既定値(<.txt値>)を取得する
Dim txtEditorName As String = ""
' レジストリ・キーを開く
Dim keyDotTxt As String = ".txt"
Dim rKeyDotTxt As RegistryKey = _
Registry.ClassesRoot.OpenSubKey(keyDotTxt)
If rKeyDotTxt IsNot Nothing Then
' レジストリの値を取得する
Dim defaultValue As String = _
CType(rKeyDotTxt.GetValue(String.Empty), String)
' レジストリ・キーを閉じる
rKeyDotTxt.Close()
txtEditorName = defaultValue
End If
If txtEditorName = Nothing Or txtEditorName = "" Then
Return ""
End If
' 「HKEY_CLASSES_ROOT\<.txt値>\shell\open\command」
' の既定値を取得する
Dim path As String = ""
' レジストリ・キーを開く
Dim keyTxtEditor As String = _
txtEditorName + "\shell\open\command"
Dim rKey As RegistryKey = _
Registry.ClassesRoot.OpenSubKey(keyTxtEditor)
If rKey IsNot Nothing Then
' レジストリの値を取得する
Dim command As String = _
CType(rKey.GetValue(String.Empty), String)
' レジストリ・キーを閉じる
rKey.Close()
If command Is Nothing Then
Return path
End If
' 前後の余白を削る
command = command.Trim()
If command.Length = 0 Then
Return path
End If
' 「"」で始まるパス形式かどうかで処理を分ける
If command(0) = """"c Then
' 「"〜"」間の文字列を抽出
Dim endIndex As Integer = command.IndexOf(""""c, 1)
If endIndex <> -1 Then
' 抽出開始を「1」ずらす分、長さも「1」引く
path = command.Substring(1, endIndex - 1)
End If
Else
' 「(先頭)〜(スペース)」間の文字列を抽出
Dim endIndex As Integer = command.IndexOf(" "c)
If endIndex <> -1 Then
path = command.Substring(0, endIndex)
Else
path = command
End If
End If