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

限于喜欢 提交于 2019-12-17 20:35:36

问题


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


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/3098486/cannot-access-files-on-drive-mapped-network-share-from-a-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!