Open a Folder/Directory and Select/Highlight a Specific File

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
 

Have you ever seen a program that can open a files destination/containing folder and also highlight/select the filename as well in the opened folder window? This will show you a simple way to do the same thing. Basically your using shell to access explorer.exe while passing certain command line arguments. All versions of Visual Basic and Visual Basic.NET can easily interface with with the explorer.exe process. VB.NET, VB 2008, Visual Basic 2010, and higher will use the same code. VB 6.0 will need an additional piece of code to open the process. The target .exe and argument text passed on are used for all VB’s however.

VB 6.0 will need to add the API shell code below to the Declarations section…

Private Declare Function ShellExecute Lib “shell32.dll” Alias “ShellExecuteA” ( ByVal hwnd As Long , ByVal lpOperation As String , ByVal lpFile As String , ByVal lpParameters As String , ByVal lpDirectory As String , ByVal nShowCmd As Long ) As Long

 

Then call the ShellExecute function and pass the proper parameter arguments.  Here is the code to open the folder and select a filename using VB 6.0…

ShellExecute  Me.hwnd, vbNullString, “explorer” , “/select,” & “c:\myFileToOpen.zip” , vbNullString, 1

 

Visual Basic .NET has built-in support for running shell based code by using the Process class. Using this class is very simple. Just add the proper arguments and parameters like below…

Process.Start( “explorer” , “/select,” & “c:\myFileToOpen.txt”)

Thats all there is to it! As you can see its a very simple code to perform this functionality. Hope this little snippet and article proves helpful to you!

Jason

Revised: 2014

One thought on “Open a Folder/Directory and Select/Highlight a Specific File

Leave a Reply