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

3

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

Othella answered 23/6, 2010 at 2:13 Comment(0)
G
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.

Gypsum answered 23/6, 2010 at 2:50 Comment(9)
Thank you for the article. Microsoft makes it clear that one should not access mapped drives from a Windows service.Othella
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.Humour
@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...)Gypsum
The statement that one "can't access mapped drives" is the issue, which is a false statement. Best practices are a different topic entirely.Humour
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.Gypsum
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?Reeher
@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, "Although the [drive mapping] APIs may return successfully, the results will be incorrect."Gypsum
@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?Reeher
@mark: As long as the link uses UNC, it should be fine.Gypsum
D
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.

Deyoung answered 23/6, 2010 at 2:22 Comment(1)
Thank you very much. I was able to convert the drive letter to UNC path using pinvoke and WNetGetConnection!Othella
P
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 [email protected]

Puffer answered 5/8, 2013 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.