Special folders
From HashVB
This article is based on Visual Basic 6. Find other Visual Basic 6 articles. |
There are numerous ways to get "special" paths in windows. You can read them directly from the registry, but this is not recommended. You can also get a few select ones by reading the environment variables, but again, these are not guaranteed. The proper way is to use the SHGetSpecialFolderLocation() function.
Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _ (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" _ (ByVal pidl As Long, ByVal pszPath As String) As Long Private Const ERROR_SUCCESS = 0 Private Const MAX_LENGTH = 260 Public Function GetSpecialFolder(ByVal CSIDL As Long) As String Dim sPath As String Dim pidl As Long 'Get the ID list form the Path ID If SHGetSpecialFolderLocation(0, CSIDL, pidl) = ERROR_SUCCESS Then 'Allocate the space for the path sPath = Space$(MAX_LENGTH) 'Get the real path form the ID list If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then 'Strip off the trailing null characters GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1) 'And add a trailing \ If Right(GetSpecialFolder, 1) <> "\" Then GetSpecialFolder = GetSpecialFolder & "\" End If End If End Function
The only exception to this rule is the temporary folder. For this, you need to make use of the GetTempPath() function.
Public Function TemporaryPath() As String Dim Data As String Dim Size As Long 'Get the size needed to store the path Size = GetTempPath(0, "") 'Create a string long enough Data = String(Size, vbNullChar) 'Get the Temporary path itself GetTempPath Len(Data), Data 'And remove the trailing Null character TemporaryPath = Left(Data, Len(Data) - 1) End Function
CSIDL folder values
The values for various folders are as follows:
Folder | Constant | Value | Example |
---|---|---|---|
Documents | |||
My Documents | CSIDL_PERSONAL | &H05 | (user)\My Documents |
My Pictures | CSIDL_MYPICTURES | &H27 | (user)\My Documents\My Pictures |
My Music | &H0D | (user)\My Documents\My Music | |
Common documents | CSIDL_COMMON_DOCUMENTS | &H2E | (all users)\Documents |
Common Pictures | &H36 | (all users)\Documents\My Pictures | |
Common Music | &H35 | (all users)\Documents\My Music | |
Common Videos | &H37 | (all users)\Documents\My Videos | |
Start menu | |||
Start menu | CSIDL_STARTMENU | &H0B | (user)\Start Menu |
Programs | CSIDL_PROGRAMS | &H02 | (user)\Start Menu\Programs |
Startup | CSIDL_STARTUP | &H07 | (user)\Start Menu\Programs\Startup |
Common start menu | CSIDL_COMMON_STARTMENU | &H16 | (all users)\Start Menu |
Common programs | CSIDL_COMMON_PROGRAMS | &H17 | (all users)\Start Menu\Programs |
Common Startup | CSIDL_COMMON_STARTUP | &H18 | (all users)\Start Menu\Programs\Startup |
Shell folders | |||
Desktop | CSIDL_DESKTOPDIRECTORY | &H10 | (user)\Desktop |
Favourites | CSIDL_FAVORITES | &H06 | (user)\Favorites |
Recent documents | CSIDL_RECENT | &H08 | (user)\Recent |
Send to... | CSIDL_SENDTO | &H09 | (user)\SendTo |
Templates | CSIDL_TEMPLATES | &H15 | (user)\Templates |
Common desktop | CSIDL_COMMON_DESKTOPDIRECTORY | &H19 | (all users)\Desktop |
Common favourites | CSIDL_COMMON_FAVORITES | &H1F | (all users)\Favourites |
Common templates | CSIDL_COMMON_TEMPLATES | &H2D | (all users)\Templates |
"Data" folders | |||
Application data | CSIDL_APPDATA | &H1A | (user)\Application Data |
Local application data | CSIDL_LOCAL_APPDATA | &H1C | (user)\Local Settings\Application Data |
Common application data | CSIDL_COMMON_APPDATA | &H23 | (all users)\Application Data |
IE folders | |||
Temporary internet files | CSIDL_INTERNET_CACHE | &H20 | |
IE Cookies | CSIDL_COOKIES | &H21 | |
IE History | CSIDL_HISTORY | &H22 | |
System folders | |||
Windows directory | CSIDL_WINDOWS | &H24 | C:\Windows |
System directory | CSIDL_SYSTEM | &H25 | C:\Windows\System32 |
Fonts | CSIDL_FONTS | &H14 | C:\Windows\Fonts |
Program files | CSIDL_PROGRAM_FILES | &H26 | C:\Program Files |
Common files | CSIDL_PROGRAM_FILES_COMMON | &H2B | C:\Program Files\Common Files |
User profile | CSIDL_PROFILE | &H28 | (user) |
Admin tools | |||
Common admin tools | CSIDL_COMMON_ADMINTOOLS | &H2F | (all users)\Start Menu\Programs\Administrative Tools |
Admin tools | CSIDL_ADMINTOOLS | &H30 | {user}\Start Menu\Programs\Administrative Tools |