关于Environ函数
我们经常接触的获取路径的API有GetWindowsDirectory()、GetSystemDirectory(),但是还有一个API叫SHGetFolderPath很少有人用,它可获取的系统路径更多,例如:
Option Explicit
Private Const MAX_PATH = 260
‘Return Values
Private Const S_FALSE = &H1
Private Const E_INVALIDARG = &H80070057
‘Flags
Private Const SHGFP_TYPE_CURRENT = 0 ‘ current value for user, verify it exists
Private Const SHGFP_TYPE_DEFAULT = 1 ‘ default value, may not exist
‘nFolder Value
Private Const CSIDL_DESKTOP = &H0 ‘
Private Const CSIDL_INTERNET = &H1 ‘ Internet Explorer (icon on desktop)
Private Const CSIDL_PROGRAMS = &H2 ‘ Start Menu\Programs
Private Const CSIDL_CONTROLS = &H3 ‘ My Computer\Control Panel
Private Const CSIDL_PRINTERS = &H4 ‘ My Computer\Printers
Private Const CSIDL_PERSONAL = &H5 ‘ My Documents
Private Const CSIDL_FAVORITES = &H6 ‘ \Favorites
Private Const CSIDL_STARTUP = &H7 ‘ Start Menu\Programs\Startup
Private Const CSIDL_RECENT = &H8 ‘ \Recent
Private Const CSIDL_SENDTO = &H9 ‘ \SendTo
Private Const CSIDL_BITBUCKET = &HA ‘ \Recycle Bin
Private Const CSIDL_STARTMENU = &HB ‘ \Start Menu
Private Const CSIDL_MYDOCUMENTS = &HC ‘ logical “My Documents” desktop icon
Private Const CSIDL_MYMUSIC = &HD ‘ “My Music” folder
Private Const CSIDL_MYVIDEO = &HE ‘ “My Videos” folder
Private Const CSIDL_DESKTOPDIRECTORY = &H10 ‘ \Desktop
Private Const CSIDL_DRIVES = &H11 ‘ My Computer
Private Const CSIDL_NETWORK = &H12 ‘ Network Neighborhood (My Network Places)
Private Const CSIDL_NETHOOD = &H13 ‘ \nethood
Private Const CSIDL_FONTS = &H14 ‘ windows\fonts
Private Const CSIDL_TEMPLATES = &H15
Private Const CSIDL_COMMON_STARTMENU = &H16 ‘ All Users\Start Menu
Private Const CSIDL_COMMON_PROGRAMS = &H17 ‘ All Users\Start Menu\Programs
Private Const CSIDL_COMMON_STARTUP = &H18 ‘ All Users\Startup
Private Const CSIDL_COMMON_DESKTOPDIRECTORY = &H19 ‘ All Users\Desktop
Private Const CSIDL_APPDATA = &H1A ‘ \Application Data
Private Const CSIDL_PRINTHOOD = &H1B ‘ \PrintHood
Private Const CSIDL_LOCAL_APPDATA = &H1C ‘ \Local Settings\Applicaiton Data (non roaming)
Private Const CSIDL_ALTSTARTUP = &H1D ‘ non localized startup
Private Const CSIDL_COMMON_ALTSTARTUP = &H1E ‘ non localized common startup
Private Const CSIDL_COMMON_FAVORITES = &H1F
Private Const CSIDL_INTERNET_CACHE = &H20
Private Const CSIDL_COOKIES = &H21
Private Const CSIDL_HISTORY = &H22
Private Const CSIDL_COMMON_APPDATA = &H23 ‘ All Users\Application Data
Private Const CSIDL_WINDOWS = &H24 ‘ GetWindowsDirectory()
Private Const CSIDL_SYSTEM = &H25 ‘ GetSystemDirectory()
Private Const CSIDL_PROGRAM_FILES = &H26 ‘ C:\Program Files
Private Const CSIDL_MYPICTURES = &H27 ‘ C:\Program Files\My Pictures
Private Const CSIDL_PROFILE = &H28 ‘ USERPROFILE
Private Const CSIDL_SYSTEMX86 = &H29 ‘ x86 system directory on RISC
Private Const CSIDL_PROGRAM_FILESX86 = &H2A ‘ x86 C:\Program Files on RISC
Private Const CSIDL_PROGRAM_FILES_COMMON = &H2B ‘ C:\Program Files\Common
Private Const CSIDL_PROGRAM_FILES_COMMONX86 = &H2C ‘ x86 Program Files\Common on RISC
Private Const CSIDL_COMMON_TEMPLATES = &H2D ‘ All Users\Templates_
Private Const CSIDL_COMMON_DOCUMENTS = &H2E ‘ All Users\Documents
Private Const CSIDL_COMMON_ADMINTOOLS = &H2F ‘ All Users\Start Menu\Programs\Administrative Tools
Private Const CSIDL_ADMINTOOLS = &H30 ‘ \Start Menu\Programs\Administrative Tools
Private Const CSIDL_CONNECTIONS = &H31 ‘ Network and Dial-up Connections
Private Const CSIDL_COMMON_MUSIC = &H35 ‘ All Users\My Music
Private Const CSIDL_COMMON_PICTURES = &H36 ‘ All Users\My Pictures
Private Const CSIDL_COMMON_VIDEO = &H37 ‘ All Users\My Video
Private Const CSIDL_RESOURCES = &H38 ‘ Resource Direcotry
Private Const CSIDL_RESOURCES_LOCALIZED = &H39 ‘ Localized Resource Direcotry
Private Const CSIDL_COMMON_OEM_LINKS = &H3A ‘ Links to All Users OEM specific apps
Private Const CSIDL_CDBURN_AREA = &H3B ‘ USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
Private Const CSIDL_COMPUTERSNEARME = &H3D ‘ Computers Near Me (computered from Workgroup membership)
Private Const CSIDL_FLAG_Create = &H8000 ‘ combine with CSIDL_ value to force folder creation in SHGetFolderPath()
Private Const CSIDL_FLAG_DONT_VERIFY = &H4000 ‘ combine with CSIDL_ value to return an unverified folder path
Private Const CSIDL_FLAG_NO_ALIAS = &H1000 ‘ combine with CSIDL_ value to insure non-alias versions of the pidl
Private Const CSIDL_FLAG_PER_USER_INIT = &H800 ‘ combine with CSIDL_ value to indicate per-user init (eg. upgrade)
Private Const CSIDL_FLAG_MASK = &HFF00 ‘ mask for all possible flag values
Private Declare Function SHGetFolderPath Lib “shell32.dll” Alias “SHGetFolderPathA” (ByVal hwndOwner As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwFlags As Long, ByVal pszPath As String) As Long
Private Sub Command1_Click()
Dim ret As Long
Dim szPath As String
szPath = String$(MAX_PATH, 0) ‘初始化buffer来准备接收路径返回值
ret = SHGetFolderPath(0, CSIDL_PROGRAM_FILES, 0, SHGFP_TYPE_CURRENT, szPath)
If (ret <> S_FALSE) or (ret <> E_INVALIDARG) Then
szPath = Left$(szPath, InStr(szPath, Chr$(0)) – 1) ‘字符串截短,去掉buffer中无用的字符
MsgBox szPath ‘显示结果
End If
End Sub
要想查询不同的路径,主要在SHGetFolderPath函数的第二个输入参数上,可查询的值见上面声名的nFolder Value部分,这些值中也包括了CSIDL_WINDOWS和CSIDL_SYSTEM,分别等效于API函数GetWindowsDirectory()和GetSystemDirectory(),SHGetFolderPath函数如果返回S_FALSE或E_INVALIDARG表示执行有错误。