How to ensure network drives are connected for an application?
Asked Answered
P

5

6

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 stored on a network drive.

Configuration files specify the path of the .MDB file on the server using a letter drive: eg. f:\data\db.mdb

The application needs to access this database file when it starts. How can I ensure the network drive is connected and accessible when the application starts?

Sometimes Windows doesn't reconnect network drives and the only way to connect them is to double-click on them in My Computer, even when "Reconnect at logon" is ticked when mapping the drive.

Would a solution be to use \\machine_name\share instead of drive letters?

Petta answered 16/1, 2012 at 5:12 Comment(2)
are you on a domain, or just a workgroup? How are the IPs distributed to your network components? Do you have the possibility to assign a fixed IP to your network drive? How do you distribute the application to the users?Badgett
@PhilippeGrondier: it's a domain. I suspect the IPs are dynamically assigned. Could I just use the machine name of the server and use UNC?Petta
Y
5

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.

Younts answered 19/1, 2012 at 17:12 Comment(0)
N
5

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:

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.

Naraka answered 24/1, 2012 at 19:52 Comment(2)
The link above is no longe ravlie.Naturalist
If the network drive is not connected, it doesnt show up in Win32_MappedLogicalDisk !Despoil
B
1

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 ...
Badgett answered 20/1, 2012 at 6:52 Comment(4)
How can you assign an IP to a disk?Petta
Well I understood that your disk was a network disk, and connects to the network through an ethernet connection, and has a MAC address. In this case, you can declare at your router level that the disk MAC address has to be associated to a specific IP. Hence, each time the disk is connected to the network, it is assigned the same IP address.Badgett
It is a network share that is mapped to a drive letter. It cannot be assigned an IP. IPs can only be assigned to be network devices.Petta
Quote : ' The application uses an.MDB database file as its database which is stored on a network drive.'. So you did not say it was a network share .. anyway if it's a network share why don't you access it throught it complete name '\\computername\foldername\filename.mdb' ?Badgett
C
0

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.

Cwm answered 16/1, 2012 at 21:32 Comment(0)
I
0

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 !

Inarticulate answered 24/1, 2012 at 0:27 Comment(1)
This will not ensure that the network drive is connected and accessible, won't it?Badgett

© 2022 - 2024 — McMap. All rights reserved.