获取文件资源管理器使用其 window 句柄显示的路径
Get path that file explorer is displaying using its window handle
我已经使用
成功获得当前文件资源管理器window的句柄
'Get handle to active window
Dim hWnd As IntPtr = GetForegroundWindow()
我没有成功找到包含上述 window 显示的路径的对象。此路径应该驻留在window的进程中,也被
获取
Dim ProcessID As UInt32 = Nothing
Dim ptr As UInt32 = GetWindowThreadProcessId(hWnd, ProcessID)
Dim Proc As Process = Process.GetProcessById(ProcessID)
然而,Proc.MainModule.Filename 仅在 IDE (VS2013) 中执行时才会将路径传递给进程。在外面执行时会突然停止。任何人都可以向我解释我不明白的地方吗?谢谢
您也许可以迭代 child windows 并获取文件名文本,但这似乎是一种困难的方法。假设 Explorer 是 ForeGroundWindow 作为起点似乎也很脆弱。这是使用 Shell32
获取 Path/FocusedFile 名称的方法
首先,打开引用 window(项目属性 -> 引用)。在 COM 选项卡中,select/check Microsoft Internet Controls 和 Microsoft Shell Controls and Automation。注意:在 Windows 8.1 中,最后一个现在命名为 Microsoft Shell Folder View Router
Imports Shell32 ' for ShellFolderView
Imports SHDocVw ' for IShellWindows
...
Private Function GetExplorerPath() As String
Dim exShell As New Shell
Dim SFV As ShellFolderView
For Each w As ShellBrowserWindow In DirectCast(exShell.Windows, IShellWindows)
' try to cast to an explorer folder
If TryCast(w.Document, IShellFolderViewDual) IsNot Nothing Then
expPath = DirectCast(w.Document, IShellFolderViewDual).FocusedItem.Path
' remove the GetDirectoryName method when you
' want to return the selected file rather than folder
Return Path.GetDirectoryName(expPath)
ElseIf TryCast(w.Document, ShellFolderView) IsNot Nothing Then
expPath = DirectCast(w.Document, ShellFolderView).FocusedItem.Path
Return Path.GetDirectoryName(expPath)
End If
Next
Return ""
End Function
使用方法:
Dim ExpPath = GetExplorerPath()
Console.WriteLine(ExpPath )
输出:
C:\Temp
我已经使用
成功获得当前文件资源管理器window的句柄'Get handle to active window
Dim hWnd As IntPtr = GetForegroundWindow()
我没有成功找到包含上述 window 显示的路径的对象。此路径应该驻留在window的进程中,也被
获取Dim ProcessID As UInt32 = Nothing
Dim ptr As UInt32 = GetWindowThreadProcessId(hWnd, ProcessID)
Dim Proc As Process = Process.GetProcessById(ProcessID)
然而,Proc.MainModule.Filename 仅在 IDE (VS2013) 中执行时才会将路径传递给进程。在外面执行时会突然停止。任何人都可以向我解释我不明白的地方吗?谢谢
您也许可以迭代 child windows 并获取文件名文本,但这似乎是一种困难的方法。假设 Explorer 是 ForeGroundWindow 作为起点似乎也很脆弱。这是使用 Shell32
首先,打开引用 window(项目属性 -> 引用)。在 COM 选项卡中,select/check Microsoft Internet Controls 和 Microsoft Shell Controls and Automation。注意:在 Windows 8.1 中,最后一个现在命名为 Microsoft Shell Folder View Router
Imports Shell32 ' for ShellFolderView
Imports SHDocVw ' for IShellWindows
...
Private Function GetExplorerPath() As String
Dim exShell As New Shell
Dim SFV As ShellFolderView
For Each w As ShellBrowserWindow In DirectCast(exShell.Windows, IShellWindows)
' try to cast to an explorer folder
If TryCast(w.Document, IShellFolderViewDual) IsNot Nothing Then
expPath = DirectCast(w.Document, IShellFolderViewDual).FocusedItem.Path
' remove the GetDirectoryName method when you
' want to return the selected file rather than folder
Return Path.GetDirectoryName(expPath)
ElseIf TryCast(w.Document, ShellFolderView) IsNot Nothing Then
expPath = DirectCast(w.Document, ShellFolderView).FocusedItem.Path
Return Path.GetDirectoryName(expPath)
End If
Next
Return ""
End Function
使用方法:
Dim ExpPath = GetExplorerPath()
Console.WriteLine(ExpPath )
输出:
C:\Temp