Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" _ (ByVal lpBuffer As String, ByVal nSize As Long) As LongDim Junk As Long Dim WinSysDir As StringWinSysDir = Space(255) Junk = GetSystemDirectory(WinSysDir, 255) WinSysDir = Left(WinSysDir, InStr(WinSysDir, Chr(0)) - 1) '获取系统目录
这个用FSO就可以 Dim fso Set fso = CreateObject("scripting.filesystemobject") MsgBox fso.GetSpecialFolder(0) MsgBox fso.GetSpecialFolder(1) Set fso = Nothing
Public Function SystemPath() As String Dim buf As String, msg As String, idx As Integer, SysPath As String idx = 1 Do buf = Environ(idx) msg = msg & buf & vbCrLfIf StrComp(Left(buf, 10), "systemroot", vbTextCompare) = 0 Then SysPath = Mid(buf, 12) SysPath = Replace(SysPath, "/", "\") If Right(msg, 1) <> "\" Then SysPath = SysPath & "\" Exit Do End If idx = idx + 1 Loop Until buf = ""If SysPath = "" Then MsgBox "程序无法得到系统目录,请和程序员联系。" End IfSystemPath = SysPath End Function
"GetWindowsDirectoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias _
"GetSystemDirectoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As LongPrivate Sub Command1_Click()
Dim WinPath As String, SysPath As String
Dim tempPath As String
Dim len5 As Long
'取得Windows 的目录
WinPath = String(255, 0)
len5 = GetWindowsDirectory(WinPath, 256)
WinPath = Left(WinPath, InStr(1, WinPath, Chr(0)) - 1)
Debug.Print "Window Path = "; WinPath'取得Windows System的目录
SysPath = String(255, 0)
len5 = GetSystemDirectory(SysPath, 256)
SysPath = Left(SysPath, InStr(1, SysPath, Chr(0)) - 1)
Debug.Print "System Path : "; SysPath'取得Temp的Directory
tempPath = String(255, 0)
len5 = GetTempPath(256, tempPath)
tempPath = Left(tempPath, len5)
Debug.Print "TEMP Path :"; tempPath
End Sub
这样可以简单的取得系统所在目录要得到系统文件夹路径要用API,如楼上
(ByVal lpBuffer As String, ByVal nSize As Long) As LongDim Junk As Long
Dim WinSysDir As StringWinSysDir = Space(255)
Junk = GetSystemDirectory(WinSysDir, 255)
WinSysDir = Left(WinSysDir, InStr(WinSysDir, Chr(0)) - 1) '获取系统目录
Dim fso
Set fso = CreateObject("scripting.filesystemobject")
MsgBox fso.GetSpecialFolder(0)
MsgBox fso.GetSpecialFolder(1)
Set fso = Nothing
Dim buf As String, msg As String, idx As Integer, SysPath As String
idx = 1
Do
buf = Environ(idx)
msg = msg & buf & vbCrLfIf StrComp(Left(buf, 10), "systemroot", vbTextCompare) = 0 Then
SysPath = Mid(buf, 12)
SysPath = Replace(SysPath, "/", "\")
If Right(msg, 1) <> "\" Then SysPath = SysPath & "\"
Exit Do
End If
idx = idx + 1
Loop Until buf = ""If SysPath = "" Then
MsgBox "程序无法得到系统目录,请和程序员联系。"
End IfSystemPath = SysPath
End Function