Tag Archives: process

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

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!


Revised: 2014

How Shutdown, Restart, and Log Off the Computer without APIs for VB 6.0 and .NET

IF you do not like using Microsoft API calls like ExitWindowsEx, but you still want to Shutdown, Restart, and/or Log Out a computer, you can use some simple shell based code. The Shell method works fine but requires the Windows Shutdown service to be active on the target computer to work. IT usually is running by default on most computers. Also using these codes won’t require your calling application to register with Windows for higher or “special” priviledges like you would if you used the ExitWindowsEx API.

  Update: I went ahead and added the VB 6.0 version of this code after the Visual Basic.NET version. The .NET code works with version Visual Basic 2008, VB 2010, and newer as well.

  Update #2: Thanks to Robbin for pointing out at the Process Class will NOT execute the Shutdown service to perform the commands. I was assuming it would and just added the required flags. So you can just use the same Shell method that VB 6.0 for .NET.

Shell "shutdown -s" 'Shutdown

Shell "shutdown -r" 'Restart

Shell "shutdown -l" 'Log Out