rootFolder 引数に任意のパスを渡す必要があるため、VB.NET で Shell.BrowseForFolder を呼び出しています。したがって、次のようにオブジェクトをインスタンス化します。
Dim shellType As Type = Type.GetTypeFromProgID("Shell.Application")
Dim shell = Activator.CreateInstance(shellType)
次に、次のように呼び出します。
Dim folder = shell.BrowseForFolder(0, message, &H241, rootFolder)
期待どおりに動作しません (ルート フォルダー F: は使用されません)。
しかし、同じ引数でリフレクションを使用すると:
Dim folder = shellType.InvokeMember("BrowseForFolder", _
BindingFlags.InvokeMethod, Nothing, shell, New Object() {0, message, &H241, _
rootFolder})
できます!
しかし、私にとっては、2 つの呼び出し (InvokeMember と直接呼び出し) は同様の結果を生成するはずです (引数は同じです)。何が起こっていますか?
編集:
実際、ToString() を呼び出すか、リテラルを配置すると機能します。
Dim folder = shell.BrowseForFolder(0, message, &H241, rootFolder.ToString())
また
Dim folder = shell.BrowseForFolder(0, message, &H241, "F:")
ただし、rootFolder が引数の場合は機能しません。たとえば、次のようになります。
Function BrowseForFolder(ByVal message As String, ByVal rootFolder As String) As String
Dim shellType As Type = Type.GetTypeFromProgID("Shell.Application")
Dim shell = Activator.CreateInstance(shellType)
Dim folder = shell.BrowseForFolder(0, message, &H241, rootFolder)
If folder Is Nothing Then
Return ""
End If
Return folder.Self.Path
End Function