找到了,分享一下吧。
Option Explicit
Dim ret As String
Dim jj As String
Dim winsys As String
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Const max_path = 260
'Private Const max_path1 = 261
Private Sub Command1_Click()
Dim ssave As String
ssave = Space(255)
ret = GetSystemDirectory(ssave, 255)
jj = Len(Trim(ssave))
winsys = Trim(Mid$(ssave, 1, jj - 1))
MsgBox winsys
End Sub
Private Sub Command2_Click()
Dim sysdir As String
Dim chrlen As Long
Dim windir As String
'sysdir变量要给足了空格
sysdir = Space(max_path)
chrlen = GetSystemDirectory(sysdir, max_path) '将系统路径保存在字符串sysdir中,字符串最大长度为260
If chrlen > max_path Then chrlen = GetSystemDirectory(sysdir, chrlen)
'chrlen是返回字符串的实际长度
sysdir = Left(sysdir, chrlen)
'-------------------------------------------------
'取windows路径
windir = Space(max_path)
chrlen = GetWindowsDirectory(windir, max_path)
If chrlen > max_path Then chrlen = GetWindowsDirectory(windir, chrlen)
windir = Left(windir, chrlen)
MsgBox "system的路径为: " & sysdir
MsgBox "windows的路径为: " & windir
MsgBox "当前系统的盘符为:" & Left(windir, 1)
End Sub