8

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#.

Elan
  • 5,548
  • 10
  • 58
  • 83

3 Answers3

10

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.

Stephen Cleary
  • 376,315
  • 69
  • 600
  • 728
  • Thank you for the article. Microsoft makes it clear that one should not access mapped drives from a Windows service. – Elan Jun 23 '10 at 17:13
  • The cited article only states that services should not use or change drive mappings, that _does not_ mean it can't be done. In the MS KB article, it even implies as such when it says: "Therefore, redirected drives cannot be shared between processes that are running under different user accounts." In other words, the login session and the service must be running under the same credentials. It CAN be done. – Garen Jun 14 '12 at 00:12
  • @Garen: Every major release of Windows increases the separation between services and desktop code, for security reasons. There *are* ways to force it to work now. There were also ways to force it on earlier Windows versions which no longer work. It's not supported; you would just create a product which may break on a future Windows version. (I'm speaking from experience...) – Stephen Cleary Jun 14 '12 at 00:26
  • The statement that one "can't access mapped drives" is the issue, which is a false statement. Best practices are a different topic entirely. – Garen Jun 14 '12 at 19:46
  • You can't access mapped drives in the same sense that you can't hide your process from Task Manager. Both are possible, but neither can be done using the provided, documented APIs. You can hack a solution to do both, and both hacks will break on different/future versions of Windows. – Stephen Cleary Jun 14 '12 at 21:31
  • And old question, but still relevant. The service in question is the TFS Build service. It builds our project, which includes a dojo build procedure, which must access a shared resource. Unfortunately, dojo build assumes everything is local, so it understands neither UNC paths nor file:// urls. Hence we resort to a mapped drive. But then the TFS Build service does not respect the mapping! Now I can delete and recreate the mapping from the build script and it works. What would you do? – mark Apr 16 '15 at 02:55
  • @mark: Your only supported options are to either fix dojo or copy the shared resource locally to your build machine as part of your build. You mentioned that "it works" to create a mapping from the service, which is partially true. It works *on this version of Windows*; this is still not supported, and Windows **has changed this behavior in the past**. To [quote](https://support.microsoft.com/en-us/kb/180362?wa=wsignin1.0), "Although the [drive mapping] APIs may return successfully, the results will be incorrect." – Stephen Cleary Apr 16 '15 at 10:07
  • @StephenCleary: I think I have another solution - a symbolic link to a shared folder using the mklink /d command. Are you aware of any reasons why it could not be an acceptable alternative? – mark Apr 16 '15 at 14:21
  • @mark: As long as the link uses UNC, it should be fine. – Stephen Cleary Apr 16 '15 at 15:13
  • @StephenCleary: it is using UNC. Thank you. – mark Apr 16 '15 at 18:03
4

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.

Sheng Jiang 蒋晟
  • 14,859
  • 2
  • 26
  • 44
  • Thank you very much. I was able to convert the drive letter to UNC path using pinvoke and WNetGetConnection! – Elan Jun 23 '10 at 17:13
0

hi elan i faced the same problem in my project and i found a solution

and is work expected follow my steps

                    if (api.Docusign_download(strDocuSignUserName, strDocuSignPassword, strDocuSignIntegratorKey, EnvelopeID, Environment.ExpandEnvironmentVariables("%temp%")) == true)
                    {
                        if (m_streamWriter1 != null)
                        {
                            m_streamWriter1.WriteLine(" This envelop id is  Downloaded and update the table" + EnvelopeID + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\n");
                        }

                        fpath1 = Environment.ExpandEnvironmentVariables("%temp%") + '\\' + EnvelopeID + '1' + ".pdf";
                        fpath2 = Environment.ExpandEnvironmentVariables("%temp%") + '\\' + EnvelopeID + '2' + ".pdf";
                        if (System.IO.File.Exists(fpath1))
                        {
                            fso = new FileSystemObject();
                            // fso.CopyFile(fileLoc, "\\\\Tech-Pro-01\\D\\", true); i download the file in temp folder and copy file to unc path ur expected work on reverse like access file to unc path he does not work directly but work in in direct access like temp folder to services
                            fso.CopyFile(fpath1, UNC, true);  
                            fso.CopyFile(fpath2, UNC, true);
                            fso.DeleteFile(fpath1, true);
                            fso.DeleteFile(fpath2, true);
                            //System.IO.File.Move(fileLoc, fileLocMove);

im just using legacy application script in vb fso file system object

1,make sure your map path access in iuser and network service access enable to the mapped provided machine 2,adding the reference system scripting

3, and unc path example \computername\sharedname\folder\filename 4,just fso.copyfile(uncpath,tempfoler,true) 5,u access a your file in temp folder he is access expected and work perfect

the temp folder access "c:\windows\temp because proceess can take the windows temp folder only

hope u elan he is work perfectly

thanks and regards

jagadeesh Govindaraj Pillai jagadeesh1492@facebook.com

Jagadeesh Govindaraj
  • 4,637
  • 6
  • 28
  • 50