Question How to control a Windows Explorer folder?

Robert Homes

Member
Joined
Nov 16, 2023
Messages
11
Programming Experience
5-10
I want to be able to issue a command in VB to open a folder in Windows Explorer and position the folder where I want it on the screen.
 
I found this post that lead me to try using this code (I modified it to move the window to the top left section of the screen with a size of 400x400). I called it using this code

MoveAllExplorerWindows:
   Sub MoveAllExplorerWindows()

       Dim ExplorerFileName As String

       For Each ExplorerWindow As SHDocVw.InternetExplorer In New SHDocVw.ShellWindows()
           ExplorerFileName = Path.GetFileNameWithoutExtension(ExplorerWindow.FullName).ToLower()
           If ExplorerFileName.ToLowerInvariant() = "explorer" Then
               ExplorerWindow.Left = 100
               ExplorerWindow.Top = 50
               ExplorerWindow.Width = 400
               ExplorerWindow.Height = 400
           End If
       Next ExplorerWindow

   End Sub

Button code:
       Process.Start("c:\windows\explorer.exe")

       ' wait a few seconds for it to load
       Dim starttime As Date = Now
       Label1.Text = "waiting 3 seconds for it to open"
       Do Until Now > DateAdd("s", 3, starttime)
           Application.DoEvents()
       Loop
       Label1.Text = "done waiting"
       Application.DoEvents()

       MoveAllExplorerWindows()

You may not need 3 seconds, I just wanted to wait long enough for the window to be open for the code to find it. On my test form I added a label to show that it was waiting to move the window, you of course need to play with the timing to suit your needs.

Be sure to follow this "Reference to this COM component to your project. The description you want to look for is Microsoft Internet Controls."

EDIT: you also need to use
VB.NET:
Imports System.Diagnostics
 
Back
Top