Home

Created By Wesley Griffin

 
 
 Retrieve Special Window Folders
 
  Home > Source Code  >  System/API > Special Folders
 
  What are special window folders?

Special window folders are folders such as the 'My Documents', 'History', 'Fonts' etc.. They are system folders, but the path of these folders may vary from user to user.  An example where you would need to find out a special windows folder would be, say you are creating a word processor application and you want the default save location to be in the users 'My Documents' folder.  You couldn't just point the file to 'c:\My Documents' as this would not apply some people.  So you must use the SHGetSpecialFolderLocation api call.

 
  Copy and paste the following code into the code window of a form:
 
 
Const CSIDL_DESKTOP = &H0     'Desktop Directory
Const CSIDL_PROGRAMS = &H2     'Program Files Directory
Const CSIDL_PERSONAL = &H5     'My Documents Directory
Const CSIDL_FAVORITES = &H6     'My Favorites Directory
Const CSIDL_RECENT = &H8     'My Recent Files Directory
Const CSIDL_STARTMENU = &HB     'The StartMenu Directory
Const CSIDL_FONTS = &H14     'Fonts Directory
Const MAX_PATH = 260
Private Type SHITEMID
     cb As Long
     abID As Byte
End Type
Private Type ITEMIDLIST
     mkid As SHITEMID
End Type
Private Declare Function ShellAbout Lib "shell32.dll" Alias "ShellAboutA" (ByVal hWnd As Long, ByVal szApp As String, ByVal szOtherStuff As String, ByVal hIcon As Long) As Long
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
 
Private Function GetSpecialfolder(CSIDL As Long) As String
     Dim r As Long
     Dim IDL As ITEMIDLIST
     r = SHGetSpecialFolderLocation(100, CSIDL, IDL)
     If r = NOERROR Then
          Path$ = Space$(512)
          r = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal Path$)
          GetSpecialfolder = Left$(Path, InStr(Path, Chr$(0)) - 1)
          Exit Function
     End If
     GetSpecialfolder = ""
End Function
 
  To get the full path of a special folder use the following syntax in a sub or function. The following example shows how to retrieve the 'My Documents' folder, but by replacing the 'CSIDL_DESKTOP' part with another constant you can return other paths.  The other constants can be found in the first seven lines of the above code. There are more paths that can be retrieved by using other constants.  To see these constants VB First Aid would recommend downloading the API-Guide and API-Viewer(VB Addin) which is available for free download at the AllApiNet website.  Please view the Links page for more details.
 
 
Dim strPath as String
strPath = GetSpecialfolder(CSIDL_DESKTOP)
 
 

 Top.

 
  Warning:
All material from this site must be used at own risk.  VB First Aid will take no responsibility for any loss of data or any other errors.  The material on this site was written for Visual Basic Version 6, therefore compatibility with other versions may cause errors.