取系統路徑

qq34352631發表於2007-10-25

取系統路徑

[@more@]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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7646028/viewspace-978528/,如需轉載,請註明出處,否則將追究法律責任。

相關文章