Results 1 to 5 of 5

Thread: check if drive is in disconnected state.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    check if drive is in disconnected state.

    Is there an API or a way to check if a drive is "disconnected network drive"?

    Thanks

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: check if drive is in disconnected state.

    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.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: check if drive is in disconnected state.

    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.

  4. #4
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: check if drive is in disconnected state.

    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

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: check if drive is in disconnected state.

    Quote Originally Posted by vbbit View Post
    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.
    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:

    Code:
    Public Function IsDriveReady(ByRef Drive As String) As Boolean
        On Error Resume Next
        IsDriveReady = CreateObject("Scripting.FileSystemObject").GetDrive(Drive).IsReady
    End Function
    You can check the DriveType property of the Drive object to see whether it is a network drive.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width