我在VB.NET中调用Shell.BrowseForFolder,因为我需要在rootFolder参数中传递任意路径。所以,我实例化一个像这样的客体:InvokeMember给出与直接调用不同的结果
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(),或者如果我把一个litteral:
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
同意,不能在vs2010上重现,同样的操作系统。 – RobS
我在Windows 7 64位操作系统上使用VS 2010。 rootFolder是一个用“F:”初始化的字符串。该磁盘存在于我的电脑上。 – Maxence
@Maxence,我想我找到了解决方法,我能够重现问题 – Fredou