Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to open "My Computer"? 3

Status
Not open for further replies.

liorlankril

Programmer
Joined
Nov 5, 2005
Messages
46
Location
IL
I have a button. I want that click on it will open to me "My Computer" in Windows browser (No Explorer).

How to do it???? What is the code?
 
Try this.
[tt]
Shell "explorer ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", vbNormalFocus[/tt]
 
How cool! Star for Hypetia. I have a question, though: how did you figure that out?? I just went into the registry and looked up the classid in the local_machine hive, but I'm not sure what entry there would tip me off that this classid represents the "my computer" folder. Can you explain?

Bob
 
These virtual folders which do not belong to file system are represented by their CLSIDs. You can query the CLSID using the Shell Controls and Automation library.

See the following code which retrieves the CLSID of various virtual folders present in Shell's namespace.
___
[tt]
Private Sub Form_Load()
With CreateObject("Shell.Application")
ScanFolder .NameSpace(0) 'Pass the desktop folder
End With
End Sub

Sub ScanFolder(Folder As Object)
Dim Item As Object
For Each Item In Folder.Items
'check if it is a virtual folder
If Left$(Item.Path, 2) = "::" Then
Debug.Print Item.Name, Item.Path
If Item.IsFolder Then ScanFolder Item.GetFolder
End If
Next
End Sub[/tt]
___

Alternatively, you can also query the CLSID of a specific folder, by its special folder constant.

Start a new project, add a reference to Microsoft Shell Controls and Automation, and try the following code.
___
[tt]
Private Sub Form_Load()
Debug.Print GetSpecialPath(ssfBITBUCKET) 'Recycle Bin
Debug.Print GetSpecialPath(ssfCONTROLS) 'Control Panel
Debug.Print GetSpecialPath(ssfDRIVES) 'My Computer
Debug.Print GetSpecialPath(ssfNETWORK) 'My Network Places
Debug.Print GetSpecialPath(ssfPRINTERS) 'Printers and Faxes
End Sub

Function GetSpecialPath(vDir As ShellSpecialFolderConstants)
With New Shell
GetSpecialPath = .NameSpace(vDir).Self.Path
End With
End Function[/tt]
___

Note that in addition to these virtual folders, you can also use this function to retrieve the path of other special file system folders (as mentioned by strongm in thread222-581363).
 
Great stuff! Is this documented somewhere?
 
Nice. I've only seen API's to get the info.

David
 
Another alternative is that you can automate the 'Windows browser' from VB. I've given two or three examples in the past in this forum, so a keyword search should find them
 
>I've only seen API's to get the info

The keyword search in this very forum would have found non-API alternatives, eg thread222-581363 shows one of my earliest public versions of GetSpecialPath from over 2 1/2 years ago ...

 
>Is this documented somewhere?

I suggest you to look at the following MSDN article.

Creating a Shell Namespace Extension

Section 2 and 3 of this article contain some specific information about invoking these special folders using standard command line, as I did in my first post.

In my second post I mentioned how to get the CLSIDs of special folders using Shell Controls and Automation library. If you use this library, you can use the built-in methods to directly open or explore a special folder, without knowing its CLSID.

Add a reference to Shell Controls and Automation and run the following code.
___
[tt]
Private Sub Form_Load()
With New Shell
.Open ssfDRIVES
End With
End Sub[/tt]
___

You can use the [tt]Explore[/tt] method instead of [tt]Open[/tt] to open the folder in Explorer view.

You can also open/explore a virtual or special file-system folder entirely using Win32 API. The idea is to get the PIDL of a special folder using GetSpecialFolderLocation function and invoke the PIDL using ShellExecuteEx function, which works on PIDLs as well. You don't need the CLSID of the folder for this method.
___
[tt]
Option Explicit
Private Declare Sub SHGetSpecialFolderLocation Lib "shell32" (ByVal hwnd As Long, ByVal csidl As Long, ByRef ppidl As Any)
Private Declare Function ShellExecuteEx Lib "shell32" (ByRef lpExecInfo As SHELLEXECUTEINFO) As Long
Private Declare Sub CoTaskMemFree Lib "ole32" (pv As Any)
Private Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Const SEE_MASK_IDLIST = &H4
Const CSIDL_DRIVES = &H11 'My Computer
Private Sub Form_Load()
OpenSpecialFolder CSIDL_DRIVES
End Sub
Sub OpenSpecialFolder(csidl As Long, Optional Explore As Boolean)
Dim sei As SHELLEXECUTEINFO
SHGetSpecialFolderLocation hwnd, CSIDL_DRIVES, sei.lpIDList
If sei.lpIDList = 0 Then Exit Sub 'folder does not exist/not created yet
sei.fMask = SEE_MASK_IDLIST
sei.cbSize = Len(sei)
sei.nShow = vbNormalFocus
If Explore Then sei.lpVerb = "explore"
ShellExecuteEx sei
CoTaskMemFree ByVal sei.lpIDList
End Sub[/tt]
___

CSIDL_DRIVES is the API equivalent of ssfDRIVES in ShellSpecialFolderConstants. You can pass any ssfXXX constant or lookup MSDN for an updated list of CSIDL constants along with their description.
 
Oops!

The line:
[tt]
SHGetSpecialFolderLocation hwnd, CSIDL_DRIVES, sei.lpIDList
[/tt]
should be read as:
[tt]
SHGetSpecialFolderLocation hwnd, csidl, sei.lpIDList
[/tt]
in my above post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top