如何使用VB6检测安装了操作系统的驱动器?
Private Sub GetSystemDrive()
' What to write here?
End Sub
一个简单的方法是使用环境变量%SystemDrive%
。您可以使用Environ
访问环境变量,例如Environ("SystemDrive")
。
如果您使用的是Win9x操作系统,则可以使用%WinDir%
,只提取驱动器部分,例如Left(Environ("WinDir"), 2)
。
使用API调用比访问环境稍微可靠一些
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Function GetSystemDrive() As String
GetSystemDrive = Space(1000)
Call GetWindowsDirectory(GetSystemDrive, Len(GetSystemDrive))
GetSystemDrive = Left$(GetSystemDrive, 2)
End Function
Private Sub Form_Load()
Debug.Print GetSystemDrive
End Sub