1

如果可能的话,我想知道如何在打开的 Windows 资源管理器窗口中获取所选文件的路径。如果没有,是否至少可以获得打开的 Windows 资源管理器窗口的文件夹路径?

我这样做的最终原因是我正在编写的软件工具需要用户选择一个文件。我想如果他们已经在使用 Windows 资源管理器移动文件然后启动我的工具,最好不要让他们在文件打开对话框中再次导航到该文件夹​​。然后,我的软件将能够识别它是否具有正确的文件扩展名,如果是,只需询问用户是否要导入该文件。

4

1 回答 1

2

所以在一些更有创意的谷歌搜索之后,我找到了一个使用 ShDocVW.dll 中的 ShellWindows 类的方法

在 VB/A 中,参考设置为 ShDocVW.dll (Microsoft Internet Controls),这是我用来获取所有打开的资源管理器窗口中所有选定文件名的集合的代码:

Function GetSelectedFilesInWinExplorers() As Collection
    Dim Result As New Collection
    Dim ExpWin As Object
    Set ExpWin = New SHDocVw.ShellWindows
    Dim CurrWin As SHDocVw.InternetExplorer
    On Error Resume Next
    Dim CurrSelFile As String
    For Each CurrWin In ExpWin
        If Not CurrWin.Document Is Nothing Then
            If Not CurrWin.Document.FocusedItem Is Nothing Then
                CurrSelFile = CurrWin.Document.FocusedItem.Path
                If CurrSelFile <> "" Then
                    Result.Add CurrSelFile
                    Debug.Print CurrSelFile
                End If
                CurrSelFile = ""
            End If
        End If
    Next CurrWin
    Set GetSelectedFilesInWinExplorers = Result
End Function

我不得不使用 On Error Resume Next,因为由于某种原因,FocusedItem 不会什么都不是,但仍会引发错误。那,我并不真正关心在这种情况下使用它。

于 2010-03-25T20:12:25.210 回答