Is there an API or a way to check if a drive is "disconnected network drive"?
Thanks
Printable View
Is there an API or a way to check if a drive is "disconnected network drive"?
Thanks
The GetDriveType function can tell you whether a drive is a remote (network) drive. You may be able to detect whether it is disconnected or not by calling the PathFileExists function. Note that you'll have to specify a file in that drive instead of the drive itself. See the Remarks section of PathFileExists' documentation for further details.
I used the getdrivetype but it keeps returning 1 and not 4 and the share itself is disconnected. It should say 4, but it does not. That's why I am hoping to find a way to detect if a drive is in disconnected state vs. it's remote drive or not. I cannot use PathFileExists because I don't know what file is in there.
You may consider something like this. I have not included the API Declares and Constants so hope you can piece those together for yourself.
Apologies in advance for the avoidable Goto (just for the pedants!)
Code:Function ValidDir(Subdir$) As Boolean
Dim Orig$, Computer$
' use of IsReachable speeds up return of False significantly when SubDir$ is on an offline networked drive
' return of True will be slightly slower
If Subdir$ Like "[A-z]:\*" Then
If GetDriveType(Left$(Subdir$, 3)) = DRIVE_REMOTE Then
Computer$ = Mid$(GetUNC(Subdir$), 3)
'most users will not be using Win 2000 so If OsVersion is placed last in the hierarchy
' it gives Win 2000 users a slightly harder time but is more efficient for others
If OsVersion() > 5 Then 'IsReachable will only work on XP and later. (Automation Error) on Win 2000
If IsReachable(Left$(Computer$, InStr(Computer$, "\") - 1), True) = 0 Then Exit Function
End If
End If
End If
Orig$ = CurDir$
On Error GoTo QuitVd
ChDir Subdir$
ValidDir = True
QuitVd:
ChDir Orig$
End Function
Function IsReachable(strComputer As String, Optional GetLast As Boolean = False) As Long ' strComputer can be name or IP address
'ref Tek-Tips thread222 -1560292
'we are primarily looking for a reliable False answer to make ValidDir$ faster
' so if a reliable False cannot be determined a value <>0 is returned and ValdidDir$ is left to do the work
Static LastComputer$, LastResult&, t!
Dim objWMIService As Object, objPing As Object, objStatus As Variant
If GetLast And Len(LastComputer$) > 0 Then
If Timer - t < 5! And LastComputer$ = strComputer Then
'if the computer id is the same as last time and less than 5 seconds have passed
IsReachable = LastResult
Else
GoTo dothework
End If
Else
dothework:
' the call is not reliable if the network is playing up and so we check if status has actually been retrieved
Dim StatusRetrieved As Boolean
On Error GoTo eh: 'can fail if WMI service is disabled
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objPing = objWMIService.ExecQuery("Select * From Win32_PingStatus Where Address = '" & strComputer & "' and StatusCode=0")
For Each objStatus In objPing
StatusRetrieved = True
IsReachable = objStatus.StatusCode = 0
Next
If Not StatusRetrieved Then IsReachable = True 'can happen if the target computer has changed its IP address
End If
LastResult& = IsReachable
LastComputer$ = strComputer
t = Timer
Exit Function
eh:
IsReachable = Err
End Function
The DRIVE_NO_ROOT_DIR (1) return value means "The root path is invalid", so if you say that the share is disconnected, then GetDriveType is returning the correct value.
You might also want to try the following (not fully-tested) function:
You can check the DriveType property of the Drive object to see whether it is a network drive.Code:Public Function IsDriveReady(ByRef Drive As String) As Boolean
On Error Resume Next
IsDriveReady = CreateObject("Scripting.FileSystemObject").GetDrive(Drive).IsReady
End Function