Difference between revisions of "Special folders"
From HashVB
m (Minor updates) |
m (Fixed a gap) |
||
(4 intermediate revisions by one user not shown) | |||
Line 6: | Line 6: | ||
Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" _ | Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" _ | ||
(ByVal pidl As Long, ByVal pszPath As String) As Long | (ByVal pidl As Long, ByVal pszPath As String) As Long | ||
+ | Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long) | ||
Private Const ERROR_SUCCESS = 0 | Private Const ERROR_SUCCESS = 0 | ||
Line 16: | Line 17: | ||
'Get the ID list from the Path ID | 'Get the ID list from the Path ID | ||
If SHGetSpecialFolderLocation(0, CSIDL, pidl) = ERROR_SUCCESS Then | If SHGetSpecialFolderLocation(0, CSIDL, pidl) = ERROR_SUCCESS Then | ||
+ | |||
'Allocate the space for the path | 'Allocate the space for the path | ||
sPath = Space$(MAX_LENGTH) | sPath = Space$(MAX_LENGTH) | ||
'Get the real path from the ID list | 'Get the real path from the ID list | ||
If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then | If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then | ||
+ | |||
'Strip off the trailing null characters | 'Strip off the trailing null characters | ||
GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1) | GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1) | ||
Line 25: | Line 28: | ||
If Right(GetSpecialFolder, 1) <> "\" Then GetSpecialFolder = GetSpecialFolder & "\" | If Right(GetSpecialFolder, 1) <> "\" Then GetSpecialFolder = GetSpecialFolder & "\" | ||
End If | End If | ||
+ | |||
+ | 'Release the ID list memory | ||
+ | CoTaskMemFree pidl | ||
End If | End If | ||
End Function | End Function | ||
Line 30: | Line 36: | ||
The only exception to this rule is the temporary folder. For this, you need to make use of the GetTempPath() function. | The only exception to this rule is the temporary folder. For this, you need to make use of the GetTempPath() function. | ||
+ | Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _ | ||
+ | (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long | ||
+ | |||
Public Function TemporaryPath() As String | Public Function TemporaryPath() As String | ||
Dim Data As String | Dim Data As String | ||
Line 174: | Line 183: | ||
* {{API link|SHGetSpecialFolderLocation}} | * {{API link|SHGetSpecialFolderLocation}} | ||
* [http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/functions/shgetspecialfolderlocation.asp MSDN page] | * [http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/functions/shgetspecialfolderlocation.asp MSDN page] | ||
− | * [http:// | + | * [http://msdn2.microsoft.com/en-us/library/bb762494.aspx CSIDL list on MSDN] |
+ | * [http://www.icetips.com/downloadfile.php?FileID=71 IceTips special folders to list all CSIDL folders on your computer] |
Latest revision as of 17:54, 30 October 2008
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 Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv 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 from 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 from 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 'Release the ID list memory CoTaskMemFree pidl 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.
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _ (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long 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 | CSIDL_MYMUSIC | &H0D | (user)\My Documents\My Music |
My Videos | CSIDL_MYVIDEO | &H0E | (user)\My Documents\My Videos |
Common documents | CSIDL_COMMON_DOCUMENTS | &H2E | (all users)\Documents |
Common Pictures | CSIDL_COMMON_PICTURES | &H36 | (all users)\Documents\My Pictures |
Common Music | CSIDL_COMMON_MUSIC | &H35 | (all users)\Documents\My Music |
Common Videos | CSIDL_COMMON_VIDEO | &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 |