Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access files on drive mapped network share from a Windows service

I have a network shared folder mapped to a drive letter, which is accessible from Windows Explorer, from the command prompt as well as from my WinForms application without problem. It is also accessible from my Windows service using a UNC path.

However, when I attempt to access this network location using a mapped drive letter from the Windows service, access fails. The Windows service is configured to use my personal "Log On" account credentials, which is the same in all the above cases. I am an administrator.

Many customer sites utilize drive letters for network shares and I cannot always control this and force them to specify UNC paths instead. I need to be able to access network shares using drive letters from a Windows service.

What do I need to do to set up my Windows service, so that it can access network shared folders that are mapped to drive letters? My Windows service is written in C#.

like image 838
Elan Avatar asked Jun 23 '10 02:06

Elan


People also ask

Can a Windows Service access a mapped drive?

Drive mappings exist across logon contexts, so if an application is running in the context of the LocalService account, then any other service running in that context may have access to the mapped drive.

Why can't I access shared folders on a network computer?

Press and hold (or right-click) the shared folder. Select Properties, and then select Advanced Sharing on the Sharing tab. Select Permissions, check Allow for Full Control of Everyone, and then press Enter.


2 Answers

Sorry; you can't access mapped drives from Windows services. As Sheng suggested, you can use a UI process to get the UNC path from a mapped drive and then pass this to the service, which must use the UNC path.

like image 78
Stephen Cleary Avatar answered Sep 29 '22 22:09

Stephen Cleary


mapped drives are per session objects. So each interactive session has its own mapping and the service session has another drive mapping. In order to get the correct UNC path of a mapped drive you need to call WNetGetConnection in the correct session.

You can use any inter-session communication methods to initiate the request and get the result in the service, such as WCF, named pipe, sockets, etc.

like image 21
Sheng Jiang 蒋晟 Avatar answered Sep 29 '22 21:09

Sheng Jiang 蒋晟