VB.NET如何打开多个位置、尺寸不同的explorer资源管理器窗口
解决方案
实现思路
- SHDocVw.ShellWindows 遍历到的Explorer窗口实例自带
LocationURL属性,可以获取当前窗口打开的文件夹路径,将其转换为本地路径后和你预设的文件夹配置匹配,即可针对不同窗口单独设置位置和尺寸。 - 提前定义好每个目标文件夹的绝对路径、坐标、尺寸映射关系,匹配后赋值对应参数即可。
前置依赖
你需要先在项目中添加COM引用:Microsoft Internet Controls(对应SHDocVw命名空间),同时导入System.IO、System.Threading命名空间。
完整示例代码
' 定义文件夹配置结构 Structure FolderWindowConfig Public FolderPath As String Public Left As Integer Public Top As Integer Public Width As Integer Public Height As Integer End Structure Sub SetExplorerWindowsPosition() ' 1. 填写你的5个文件夹实际绝对路径和对应参数 Dim configs As New List(Of FolderWindowConfig) From { New FolderWindowConfig With {.FolderPath = "C:\你的文件夹1完整路径", .Left = 5, .Top = 5, .Width = 100, .Height = 150}, New FolderWindowConfig With {.FolderPath = "C:\你的文件夹2完整路径", .Left = 50, .Top = 5, .Width = 80, .Height = 75}, New FolderWindowConfig With {.FolderPath = "C:\你的文件夹3完整路径", .Left = 100, .Top = 5, .Width = 100, .Height = 50}, New FolderWindowConfig With {.FolderPath = "C:\你的文件夹4完整路径", .Left = 150, .Top = 5, .Width = 35, .Height = 90}, New FolderWindowConfig With {.FolderPath = "C:\你的文件夹5完整路径", .Left = 200, .Top = 5, .Width = 50, .Height = 35} } ' 2. 先启动所有目标文件夹的Explorer窗口 For Each cfg In configs Process.Start("explorer.exe", cfg.FolderPath) Next ' 等待窗口加载完成,可根据实际情况调整延迟时间 Thread.Sleep(1000) ' 3. 遍历所有Explorer窗口匹配配置 For Each ExplorerWindow As SHDocVw.InternetExplorer In New SHDocVw.ShellWindows() Dim exeName = Path.GetFileNameWithoutExtension(ExplorerWindow.FullName).ToLowerInvariant() If exeName <> "explorer" Then Continue For ' 将URL格式的路径转换为本地绝对路径 Dim currentFolderPath = "" Try Dim uri As New Uri(ExplorerWindow.LocationURL) currentFolderPath = uri.LocalPath.TrimEnd(Path.DirectorySeparatorChar) Catch ex As Exception ' 跳过特殊窗口(比如此电脑、回收站等非文件夹路径窗口) Continue For End Try ' 匹配配置设置参数 For Each cfg In configs Dim cfgPath = cfg.FolderPath.TrimEnd(Path.DirectorySeparatorChar) If String.Equals(currentFolderPath, cfgPath, StringComparison.OrdinalIgnoreCase) Then ExplorerWindow.Left = cfg.Left ExplorerWindow.Top = cfg.Top ExplorerWindow.Width = cfg.Width ExplorerWindow.Height = cfg.Height Exit For End If Next Next End Sub
注意事项
- 路径要填写完整的绝对路径,避免相对路径匹配失败。
- 如果启动后窗口加载较慢,可以适当调大
Thread.Sleep的毫秒值,或者增加循环判断窗口是否存在的逻辑避免遗漏。 - 如果需要避免调整已经打开的其他Explorer窗口,可以在启动新窗口前先记录已有窗口的HWND,后续遍历的时候排除即可。
内容的提问来源于stack exchange,提问作者JojoStyle
相关产品推荐
相关产品推荐

