Library code snippets
Special Folder Paths
Throughout Windows there are now a large number of 'special' folders such as My Documents, the Start Menu, Program Files etc. Unfortunately, the location of these important folders can vary, so we need to use the SHGetSpecialFolderLocation API to accurately find them. This code shows you how.
'Module Code
Option Explicit
Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" _
(ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA"
_
(ByVal pidl As Long, ByVal pszPath As String) As Long
Public Type SHITEMID
cb As Long
abID As Byte
End Type
Public Type ITEMIDLIST
mkid As SHITEMID
End Type
Public Const MAX_PATH As Integer = 260
Public Function fGetSpecialFolder(CSIDL As Long) As String
Dim sPath As String
Dim IDL As ITEMIDLIST
'
' Retrieve info about system folders such as the "Recent
Documents" folder.
' Info is stored in the IDL structure.
'
fGetSpecialFolder = ""
If SHGetSpecialFolderLocation(Form1.hwnd, CSIDL, IDL) = 0
Then
'
' Get the path from the ID list,
and return the folder.
'
sPath = Space$(MAX_PATH)
If SHGetPathFromIDList(ByVal IDL.mkid.cb,
ByVal sPath) Then
fGetSpecialFolder
= Left$(sPath, InStr(sPath, vbNullChar) - 1) & ""
End If
End If
End Function
'Form Code
Private Const CSIDL_DESKTOP = &H0 '// The Desktop - virtual folder
Private Const CSIDL_PROGRAMS = 2 '// Program Files
Private Const CSIDL_CONTROLS = 3 '// Control Panel - virtual folder
Private Const CSIDL_PRINTERS = 4 '// Printers - virtual folder
Private Const CSIDL_DOCUMENTS = 5 '// My Documents
Private Const CSIDL_FAVORITES = 6 '// Favourites
Private Const CSIDL_STARTUP = 7 '// Startup Folder
Private Const CSIDL_RECENT = 8 '// Recent Documents
Private Const CSIDL_SENDTO = 9 '// Send To Folder
Private Const CSIDL_BITBUCKET = 10 '// Recycle Bin - virtual folder
Private Const CSIDL_STARTMENU = 11 '// Start Menu
Private Const CSIDL_DESKTOPFOLDER = 16 '// Desktop folder
Private Const CSIDL_DRIVES = 17 '// My Computer - virtual folder
Private Const CSIDL_NETWORK = 18 '// Network Neighbourhood - virtual folder
Private Const CSIDL_NETHOOD = 19 '// NetHood Folder
Private Const CSIDL_FONTS = 20 '// Fonts folder
Private Const CSIDL_SHELLNEW = 21 '// ShellNew folder
Private Sub form_load()
MsgBox "Desktop Folder " & fGetSpecialFolder(CSIDL_DESKTOPFOLDER)
MsgBox "Recent Folder " & fGetSpecialFolder(CSIDL_RECENT)
'// etc...
End Sub
Comments
Related articles
Related discussion
-
VB6, SQL 2005 & DMO
by elajaunie3 (1 replies)
-
sending sms from pc
by sriraj20074 (0 replies)
-
Automating Excel from VB6.0
by epurdy (0 replies)
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
-
video not working with visual basic
by Jupiter 2 (9 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Thank you for your source code wich has helped me.
'***************************************************************************
'A really shorthand and simple way to get the WIN SYS and TMP paths with API
'For the beginners and someone asked so here it is...
'Place the following code in a module and call as usual...
'***************************************************************************
Option Explicit
Public Const MAX_PATH As Long = 260
Public Declare Function GetWindowsDirectory Lib "kernel32" Alias _
"GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Declare Function GetSystemDirectory Lib "kernel32" Alias _
"GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Declare Function GetTempPath Lib "kernel32" Alias _
"GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Public Function GetWINpath() As String
Dim sBuffer As String * MAX_PATH
GetWINpath = Left(sBuffer, GetWindowsDirectory(sBuffer, MAX_PATH))
End Function
Public Function GetSYSpath() As String
Dim sBuffer As String * MAX_PATH
GetSYSpath = Left(sBuffer, GetSystemDirectory(sBuffer, MAX_PATH))
End Function
Public Function GetTMPpath() As String
Dim sBuffer As String * MAX_PATH
GetTMPpath = Left(sBuffer, GetTempPath(255, sBuffer))
End Function
Private Const CSIDL_PROGRAMS = 2 '// Program Files
to get the path of program files folder path use CSIDL_PROGRAMS_FILES macro with a value &h26.
Are there anymore path the API function can find? I'm looking for the windows path, the system path, the program files folder path and the drive letter of where windows is installed path. Please help me out.
This thread is for discussions of Special Folder Paths.
Leave a comment
Sign in or Join us (it's free).
Related tags
visual basic