Dev
Dev

Reputation: 360

How to find the operating system drive

How can I detect the drive on which the operating system is installed using VB6?

Private Sub GetSystemDrive()
    ' What to write here?
End Sub

Upvotes: 2

Views: 3521

Answers (2)

bobbymcr
bobbymcr

Reputation: 24177

An easy way is to use the environment variable %SystemDrive%. You can access environment variables using Environ, e.g. Environ("SystemDrive").

If you are on a Win9x OS, you can use %WinDir% and just extract the drive portion, e.g. Left(Environ("WinDir"), 2).

Upvotes: 4

wqw
wqw

Reputation: 11991

Using API calls is slightly more reliable than accessing environment

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

Upvotes: 3

Related Questions