Tag Archives: shell

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

How to Open a Url or Link in Default WebBrowser – VB 6.0

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

I figured this would be very easy to do since VB has a Shell command built-in. But I would keep getting a File not Found message everytime I would try and open a web address. It would work for other tasks like shelling “shutdown -r -f -t 0” which is a process that can be be used to shutdown or restart the computer, but not opening a URL. So I decided to go ahead and use the Windows APIs to do this task. Below is the API Call and codes to open a url in the default web browser.

____________________________________

This Function will be used to process the url which will have windows open the web browser pointing to that url path.

    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

Now all thats needed is to execute that function passing the url you want opened in the third parameter. For this task the 3rd parameter is the most important. Well, I guess 2, 3, 6 should be set. Parameter #2 is how to process the task in the 3rd parameter which will be set to “open”. You could just put a blank string like “” which will still open the url. But just enter “open” to be on the safe side. Parameter #6 is the window style to open the application. Like 1 is for normal and such. As far as I know the other paramters is not required for this to work so I just set them to 0 or vbnullstring.

        '
        'In the last parameter you can specify the program window style.
        '0 = Hide, 1 = Normal, 2 = Minimize, or 3 = Maximize.
        '
        'For other styles just open the VB API Viewer application under your VB's Tools directory and open the Win32Api.txt file and check out the constants that starts with "SW" like SW_NORMAL.
        '
        'Anyways, this will have the Windows Shell open the Default WebBrowser with the url specified in the 3rd parameter.
        '
        ShellExecute 0, "open", "http://www.vbcodesource.info", vbNullString, vbNullString, 1

____________________________________

I added the next codes just to show how to open another program and pass arguments to the program. This code will launch the FireFox webbrowser and open the URL in the 4th parameter.

 

        '
        'This simply shows how to open a specific program like Firefox which may not be the default webbrowser
        'and how to get it to go to a specific url.
        ShellExecute 0, "open", "C:\Program Files\Mozilla Firefox\Firefox.exe", "http://www.msdn.com", vbNullString, 1

 

____________________________________________

Thats all there is to it!. As you can see with a simple API call you can get the job done. Anyways, have fun!

Jason

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

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

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