You are currently offline, waiting for your internet to reconnect

How To Use the SHGetFolderPath Function from Visual Basic

This article was previously published under Q252652
SUMMARY
You can use the SHGetFolderPath function to get the path to Microsoft Windows special folders, which are identified by their CSIDL value. This article includes a code example on using the SHGetFolderPath function from Microsoft Visual Basic to get the path of the logged-in user's Application Data folder. A typical path for this folder might be:
C:\Winnt\Profiles\Broder\Local Settings\Application Data
MORE INFORMATION
SHGetFolderPath is a new API function for Windows 2000. If you are not running Windows 2000, then the SHGetFolderPath function requires that you redistribute the SHFolder.dll file. The SHFolder.dll file is freely redistributable and can be obtained from the latest Platform Software Development Kit (SDK), which is available from the following Web site: Because the location of special folders can change from system to system, Microsoft recommends that you use the SHGetFolderPath function to determine the path for a special folder. For example, you might want to determine the user's Application Data folder to store user-specific data for your application. You can determine this at run time using the SHGetFolderPath function.

Please refer to the MSDN documentation to determine which folders are supported by the SHGetFolderPath function.

As an alternative, if you set a reference to Microsoft Scripting Runtime in the References dialog box, you can use the FileSystemObject's GetSpecialFolder method to obtain the locations of special folders.

Step-by-Step Example

  1. In Visual Basic, start a new Standard EXE project. Form1 is created by default.
  2. Add a CommandButton and a TextBox control to Form1.
  3. In the General Declarations Section of the code window for Form1, paste the following code:
    Option ExplicitPrivate Const S_OK = &H0                ' SuccessPrivate Const S_FALSE = &H1             ' The Folder is valid, but does not existPrivate Const E_INVALIDARG = &H80070057 ' Invalid CSIDL ValuePrivate Const CSIDL_LOCAL_APPDATA = &H1C&Private Const CSIDL_FLAG_CREATE = &H8000&Private Const SHGFP_TYPE_CURRENT = 0Private Const SHGFP_TYPE_DEFAULT = 1Private Const MAX_PATH = 260Private Declare Function SHGetFolderPath Lib "shfolder" _    Alias "SHGetFolderPathA" _    (ByVal hwndOwner As Long, ByVal nFolder As Long, _    ByVal hToken As Long, ByVal dwFlags As Long, _    ByVal pszPath As String) As LongPrivate Sub Command1_Click()Dim sPath As StringDim RetVal As Long' Fill our string buffersPath = String(MAX_PATH, 0)RetVal = SHGetFolderPath(0, CSIDL_LOCAL_APPDATA Or CSIDL_FLAG_CREATE, 0, SHGFP_TYPE_CURRENT, sPath)Select Case RetVal    Case S_OK        ' We retrieved the folder successfully                ' All C strings are null terminated        ' So we need to return the string upto the first null character        sPath = Left(sPath, InStr(1, sPath, Chr(0)) - 1)        Text1.Text = sPath    Case S_FALSE        ' The CSIDL in nFolder is valid, but the folder does not exist.        ' Use CSIDL_FLAG_CREATE to have it created automatically        MsgBox "The folder does not exist"    Case E_INVALIDARG        ' nFolder is invalid        MsgBox "An invalid folder ID was specified"    End SelectEnd Sub					
  4. Press the F5 key to run the project.
  5. Click the CommandButton control. The TextBox control is filled with the path to the current user's Application Data folder.
Note that the CSIDL_FLAG_CREATE flag is used. If the folder does not exist, then the SHGetFolderPath function creates it for you, fills the string buffer with the path, and returns S_OK. If you do not use the CSIDL_FLAG_CREATE flag, and the folder does not exist, then the SHGetFolder function returns S_FALSE and nothing is placed in your string buffer. To find the location of other special folders, you need to change the nFolder parameter to another CSIDL value. The constants for these values can be found on the MSDN.
REFERENCES
MSDN Documentation on SHGetFolderPath
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
227051 How To Determine the Location of Files or Folders on Windows 2000
SHGetSpecialFolderPath shell shlobj.h
Properties

Article ID: 252652 - Last Review: 08/27/2007 08:11:05 - Revision: 2.6

  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 4.0 Enterprise Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • kbapi kbhowto kbshellgrp KB252652
Feedback