(Print this page)

Mapping network folder
Published date: Sunday, January 20, 2008
On: Moer and Éric Moreau's web site

Sometimes, your application needs to map and unmap network drives automatically in order to process files. It is also possible that the application needs to use resources that users themselves are not normally allowed to access.

This is the kind of situation where my article can help you. It is about mapping and unmapping network drives. I will show you how to map and unmap drives directly from your code without having the user to know what is happening (see the ConnectDrive and DisconnectDrive methods). I will also show you how to display standards dialogs to the user (see the DisplayDialog method).

Feature is not built-in into the .Net Framework

Download the code attached to this article and you will find a class (cNetworkUtil.vb) containing the reusable code of this demo. The form that is also included with the demo application is only used to call methods of the DLL.

Figure 1:The demo application in action

None of the features explained here are available directly through the .Net Framework. That’s why we need to declare functions available from the MPR DLL. Because this DLL is only exposing as a module, we need to DECLARE the functions it contains to our application (there is no need to add a reference to it). See the top of the class for the various declarations.

What is mpr.dll?

mpr.dll is a DLL provided by Windows. MPR stands for Multiple Provider Router. It is a module containing functions used to handle communication between the Windows operating system and the installed network providers.

Mapping

The ConnectDrive method of the class is using WNetAddConnection2 method of the MPR DLL. It requires a structure (see the NETRESOURCE structure in the cNetworkUtil.vb class). While most of properties of this structure aren’t really required, this is where you will provide the path you want to connect to and the local drive letter you want to use. Other parameters that are required by the method are the user name and the password.

Be sure to trap exceptions that can be thrown by the ConnectDrive method because many can happen (invalid path, local drive already in use, invalid credentials ...).

Unmapping

The DisconnectDrive is even simpler. You simply pass the drive letter you want to disconnect and a Boolean value to indicate if you want to force disconnect. This method is using the WNetCancelConnection2 method of the MPR DLL.

Again, be sure to trap exceptions thrown by this method.

Showing the standard Connect/Disconnect dialogs

Sometime, you need to let the user enter their own credentials and they don’t like the idea of having you being able to capture it. Under those circumstances, instead of building your own dialog, you can reuse those available in the MPR DLL.

The WNetConnectionDialog method and the WNetDisconnectDialog method are the methods available in the MPR DLL that displays the standard Windows dialogs. Any user who already have connected or disconnected a network folder using Windows will recognize it.

Conclusion

Our application sometime needs to access resources that are not local. Sometimes, the developer is the only one to know the credentials to connect to these resources while other time it is the user that knows them.

Using the simple class here, you can connect your applications to those resources whatever the way used in your business.


(Print this page)