简体   繁体   English

无法从 Windows 服务访问驱动器映射网络共享上的文件

[英]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.我有一个映射到驱动器号的网络共享文件夹,它可以从 Windows 资源管理器、命令提示符以及我的 WinForms 应用程序访问,没有问题。 It is also accessible from my Windows service using a UNC path.也可以使用 UNC 路径从我的 Windows 服务访问它。

However, when I attempt to access this network location using a mapped drive letter from the Windows service, access fails.但是,当我尝试使用来自 Windows 服务的映射驱动器号访问此网络位置时,访问失败。 The Windows service is configured to use my personal "Log On" account credentials, which is the same in all the above cases. Windows 服务配置为使用我的个人“登录”帐户凭据,在上述所有情况下都是相同的。 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.许多客户站点将驱动器号用于网络共享,我不能总是控制它并强制他们指定 UNC 路径。 I need to be able to access network shares using drive letters from a Windows service.我需要能够使用来自 Windows 服务的驱动器号访问网络共享。

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?我需要做什么来设置我的 Windows 服务,以便它可以访问映射到驱动器号的网络共享文件夹? My Windows service is written in C#.我的 Windows 服务是用 C# 编写的。

Sorry;对不起; you can't access mapped drives from Windows services . 您无法从 Windows 服务访问映射驱动器 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.正如盛所建议的那样,您可以使用 UI 进程从映射驱动器获取 UNC 路径,然后将其传递给必须使用 UNC 路径的服务。

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.为了获得映射驱动器的正确 UNC 路径,您需要在正确的会话中调用 WNetGetConnection。

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.您可以使用任何会话间通信方法来发起请求并在服务中获取结果,例如 WCF、命名管道、套接字等。

hi elan i faced the same problem in my project and i found a solution嗨 elan 我在我的项目中遇到了同样的问题,我找到了一个解决方案

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我只是在 vb fso 文件系统对象中使用遗留应用程序脚本

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 1,确保您在 iuser 中的地图路径访问和网络服务访问启用到映射提供的机器 2,添加参考系统脚本

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 3,和 unc 路径示例 \\computername\\sharedname\\folder\\filename 4,只是 fso.copyfile(uncpath,tempfoler,true) 5,你访问你的临时文件夹中的文件他是预期的访问和工作完美

the temp folder access "c:\\windows\\temp because proceess can take the windows temp folder only临时文件夹访问“c:\\windows\\temp”,因为进程只能获取 Windows 临时文件夹

hope u elan he is work perfectly希望你elan他工作得很好

thanks and regards感谢致敬

jagadeesh Govindaraj Pillai jagadeesh1492@facebook.com jagadeesh Govindaraj Pillai jagadeesh1492@facebook.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM