Programming » Visual Basic 6 » Visual Basic 6 Code » File and Disk access » ");?>
Get the path of most of the special folders
'To get the path to the Windows directory:
Declare Function GetWindowsDirectory Lib "kernel32" _
        Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _
        ByVal nSize As LongAs Long
Function WindowsDir() As String
    Dim x As Long
    Dim strPath As String
    strPath = Space$(1024)
    x = GetWindowsDirectory(strPath, Len(strPath))
    strPath = Left$(strPath, x)
    If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    WindowsDir = strPath
End Function

' To find the Windows\System directory path:
Declare Function GetSystemDirectory Lib "kernel32" _
        Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _
        ByVal nSize As LongAs Long
Function SystemDir() As String
    Dim x As Long
    Dim strPath As String
    strPath = Space$(1024)
    x = GetSystemDirectory(strPath, Len(strPath))
    strPath = Left$(strPath, x)
    If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    SystemDir = strPath
End Function

' To get other system directories:
Public Enum SystemFolder
    CSIDL_DESKTOP = 0
    CSIDL_INTERNET = 1
    CSIDL_PROGRAMS = 2
    CSIDL_CONTROLS = 3
    CSIDL_PRINTERS = 4
    CSIDL_PERSONAL = 5
    CSIDL_FAVORITES = 6
    CSIDL_STARTUP = 7
    CSIDL_RECENT = 8
    CSIDL_SENDTO = 9
    CSIDL_BITBUCKET = 10
    CSIDL_STARTMENU = 11
    CSIDL_DESKTOPDIRECTORY = 16
    CSIDL_DRIVES = 17
    CSIDL_NETWORK = 18
    CSIDL_NETHOOD = 19
    CSIDL_FONTS = 20
    CSIDL_TEMPLATES = 21
    CSIDL_COMMON_STARTMENU = 22
    CSIDL_COMMON_PROGRAMS = 23
    CSIDL_COMMON_STARTUP = 24
    CSIDL_COMMON_DESKTOPDIRECTORY = 25
    CSIDL_APPDATA = 26
    CSIDL_PRINTHOOD = 27
    CSIDL_ALTSTARTUP = 29
    CSIDL_COMMON_ALTSTARTUP = 30
    CSIDL_COMMON_FAVORITES = 31
    CSIDL_INTERNET_CACHE = 32
    CSIDL_COOKIES = 33
    CSIDL_HISTORY = 34
End Enum

Declare Function SHGetSpecialFolderLocation Lib "Shell32.DLL" _
        (ByVal hWndOwner As LongByVal SHFolder As Long, _
        idl As LongAs Long

Declare Function SHGetPathFromIDList Lib "Shell32.DLL" _
        (ByVal idl As LongByVal Path As StringAs Long
Declare Function GetDesktopWindow Lib "User32.DLL" () As Long

Function SystemPath(ByVal PathID As SystemFolder) As String
    Dim lngIDL As Long    ' directory IDL value
    Dim strBuff As String
    strBuff = Space$(1024)
    Dim n As Long
    n = SHGetSpecialFolderLocation(GetDesktopWindow(), PathID, lngIDL)
    If n Then Exit Function
    n = SHGetPathFromIDList(lngIDL, strBuff)
    If n > 0 Then
        n = InStr(strBuff, Chr$(0)) - 1
        strBuff = Left$(strBuff, n)
        If Right$(strBuff, 1) <> "\" Then strBuff = strBuff & "\"
        SystemPath = strBuff
    End If
End Function