Friday, July 02, 2010

Map Network folder (UNC path) from Windows service

This post explains how to map the network folder path(UNC Path) from windows services. The UNC path may be in same machine or same network domain or different domain.


Introduction


This article explains how to map the network folder path(UNC Path) from windows services. The UNC path may be in same machine or same network domain or different domain.

Background

As per my knowledge there is no Managed API to attach and detach UNC network path which is in same or another domain to the local system. Also there is a limitation to access the attached drive from Windows service. This is why i have written this code snippets. Actually i have planned to write a functions for most of the MPR.dll functions. But due to time constraint i have implemented only two API's. I will update this article with new functions, when i get time. Also i am expecting your valuable comments regarding the way i implemented the code and the way i used design patterns. Forgive me, if i said anything wronly :)


Using the code

Before get into the code, i will give some information about windows service.

A Windows service is an application that starts when Windows is booted and runs in the background as long as Windows is running. Windows services by default are run as a virtual user: "LocalSystem" that has administrative rights on the system. The working directory will be the Windows system directory (typically C:\WINNT) and the default temp directory will typically be C:\WINNT\TEMP. Since this is not a real user, this presents some challenges if user-specific data needs to be stored by the application, as there is no home directory for this user. LocalSystem also has no access to network file shares and similar resources; if a service needs to access files on the network, it generally needs to be configured to run as a domain user with access to those files.

So if your windows service needs to access files from shared drive which is in another domain, then you need map the drive using the built-in user credentials through program. This code snippets will be used to map drive. The NetworkHelperStruct.cs partial class contains all the unmanaged flags and extern declaration of unmanaged api which is in mpr.dll and NetworkHelper.cs file contains the implemented code for those api. The WNetAddConnection function will map the drive and WNetCancelConnection will disconnect the drive. The properties.xml is used to configure the networkpath, localdrive, username and password. The below section is excerpt from config xml. you can add more section based on your functionalities.

Screenshot - Helperapi_config.gif

Sample code to use networkhelper:

NetworkHelper networkhelper;
        networkhelper = NetworkHelper.GetInstance ();
        bool result;
        result = networkhelper.MapDrive ("M:", true);
        result = networkhelper.MapDrive ("N:", true);
        result = networkhelper.MapDrive ("X:", true);
        result = networkhelper.WNetCancelConnection("X:",false);
        //result = networkhelper.WNetAddConnection ("K:", remote, "meenakshisundaram", "", false);
        //result = networkhelper.WNetCancelConnection("K:",false);


You can get the source code from my article where i have posted in codeproject

 http://www.codeproject.com/KB/system/Map_Network_folder_UNC_.aspx

Happy coding.

No comments: