Click here to Skip to main content
Licence 
First Posted 6 Apr 2007
Views 80,187
Downloads 1,363
Bookmarked 23 times

Map Network folder (UNC path) from Windows service

By | 6 Apr 2007 | Article
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.

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);

I have attached the source code of Networkhelper and a sample solution to use the class.

Points of Interest

I am planning to write a wrapper class for mpr.dll, once i get a time.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Suresh Meenakshisundaram

Software Developer (Senior)

India India

Member

I am a senior software engineer with 5.5 years of experiance, currently working in Bangalore. I have worked in C#, SQL Server 2005, Oracle 9i, VB6.0, SQL with DTS, NAnt & NUnit. Also i worked in interfaces like iDoc, CIF and HL7.
 
My interests are hearing music, playing carrom,cricket and reading articles.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralVb.NET 1.1 PinmemberRamanSharma4:40 2 Sep '09  
Generalproperties.xml PinmemberPatsAvila14:12 25 Apr '09  
GeneralIt doesn't work :( Pinmemberzwoke0:04 11 Jan '08  
GeneralWill it work on windows 2003 PinmemberPerlDev5:07 29 Nov '07  
QuestionService Wont Start Pinmemberfunkymint16:20 26 Nov '07  
AnswerRe: Service Wont Start Pinmemberfunkymint9:23 1 Dec '07  
GeneralAccessing a Network folder already mapped (e.g. Z: ) Pinmemberedaram13:27 21 Nov '07  
GeneralRe: Accessing a Network folder already mapped (e.g. Z: ) PinmemberSuresh Meenakshisundaram16:33 21 Nov '07  
GeneralRe: Accessing a Network folder already mapped (e.g. Z: ) Pinmemberedaram4:14 22 Nov '07  
GeneralRe: Accessing a Network folder using windows service (e.g. Z: ) PinmemberRam. S23:38 8 Oct '09  
Generalclass with securty features Pinmemberaejw13:52 5 Oct '07  
GeneralRe: class with securty features PinmemberSuresh Meenakshisundaram18:21 5 Oct '07  
QuestionAcces denied Pinmembernajonemde6:57 2 Aug '07  
AnswerRe: Acces denied [modified] PinmemberSuresh Meenakshisundaram21:01 13 Aug '07  
QuestionCannot compile project Pinmemberhk115:40 9 Jul '07  
AnswerRe: Cannot compile project PinmemberSuresh Meenakshisundaram2:20 28 Jul '07  
GeneralIs not working Pinmemberddniga2:00 9 May '07  
GeneralRe: Is not working Pinmemberjason5418:29 16 May '07  
GeneralRe: Is not working PinmemberSuresh Meenakshisundaram20:37 26 Jul '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 7 Apr 2007
Article Copyright 2007 by Suresh Meenakshisundaram
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid