How to ensure network drives are connected for an application?

前端 未结 5 1987
小蘑菇
小蘑菇 2021-01-18 15:03

I have a desktop Windows application that is installed in small office environments.

The application uses an .MDB database file as its database which is

相关标签:
5条回答
  • 2021-01-18 15:42

    You should use UNC paths, because not everyone will have your drive mapped to the same letter.

    Determine UNC path

    First, I would determine the UNC path of your file as it exists on your local computer at F:\data\db.mdb using one of the techniques found here:

    • Creating UNC paths from mapped drives

    Basically, you look at the way Windows Explorer lists the network mapped drive, then use this to deduce the UNC path.

    Check Availability using WMI

    Assuming the drive is actually mapped on every local computer that plans to use the application, use the Win32_MappedLogicalDisk class to determine availability of the mapped network drive.

    I have some sample code here that can be adapted to determine whether a given network drive is available (scroll down to the Mapped Drives Information section). You check .ProviderName to match the UNC path, so you know which is the correct drive, then check the value of .Availability to determine if the mapped network drive can be accessed.

    0 讨论(0)
  • 2021-01-18 15:46

    Make sure this script is ran right before the application is started:

    net use f: \\machine_name\share /user:[username] [password] /persistent:yes
    

    This will map the share drive on the letter you specified !

    0 讨论(0)
  • 2021-01-18 15:59

    You should definitely abandon the network drive mapping possibilities:

    • using this technique forces you to manipulate 'physically' each computer using your db, where you have to assign a letter to the network drive.
    • every computer user can easily change it
    • any disconnection from the network might force the user to 'manually' reconnect to the disk drive

    Though you are on a domain, I would not advise you to use a name, as computers, for multiple reasons, might not always find it 'easily' on the network, specially when its IP is regularly changed.

    You should definitely find a way to assign a fixed IP to your disk: it is the most stable and permanent solution you can think about. Ask your domain administrator to arrange it for you.

    Testing the presence of your network disk can then be done very easily. There are multiple solutions, including trying to open directly the mdb file. You could also test the existence of the file (through the file object I think) or even use any external program or windows API that you could launch from your code. Please google 'VB test IP' or something similar to find a solution at your convenience.

    EDIT: windows has even a proposal to simulate a ping with some VB code. Check it here

    EDIT2: I found in one of my apps this VBA code, which allows to quick-check if a file exists (and can ba accessed) somewhere on your network. It was basically set to test if a new version of the user interface is available.

    Function fileIsAvailable(x_nom As Variant) As Boolean
    
    On Error GoTo ERREUR
    Application.Screen.MousePointer = 11
    
    Dim fso as object
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FileExists(x_nom) Then
        fileIsAvailable = False
    Else
        fileIsAvailable = True
    End If
    Set fso = Nothing
    
    Application.Screen.MousePointer = 0
    On Error GoTo 0
    Exit Function
    
    ERREUR:
    Application.Screen.MousePointer = 0
    debug.print Err.Number, Err.description
    End Function 
    

    You can easily call this function by supplying your file's network name, such as:

    if fileIsAvailable("\\192.168.1.110\myFileName.mdb") then ...
    
    0 讨论(0)
  • 2021-01-18 16:01

    You asked, "Would a solution be to use \machine_name\share instead of drive letters?"

    I think, yes, it could be. A UNC path avoids 2 problems:

    1. share not connected to a drive letter
    2. share is connected, but mapped to a different drive letter than you expect

    The unknown is whether anything in your application makes a UNC path for the MDB either a complication or a flat out deal-breaker.

    0 讨论(0)
  • 2021-01-18 16:01

    You did not make it clear what your application was written in, however before you attempt to connect to the database for the first time, presumably in a splash screen or something of that nature, check that f:\data\db.mdb exists.

    0 讨论(0)
提交回复
热议问题