2012-03-29 54 views
3

我有一个Windows服务,用于轮询特定文件夹以创建新文件。当文件夹位于本地驱动器之一(如C:或D: )时,此功能正常工作无法在映射的驱动器上找到文件夹。从用C编写的Windows服务访问映射文件夹

下面是执行检查的文件夹中的代码投票前存在:

System.Security.Principal.WindowsIdentity userIdentity = 
      System.Security.Principal.WindowsIdentity.GetCurrent(); 
      System.Security.Principal.WindowsPrincipal principal = 
       new System.Security.Principal.WindowsPrincipal(userIdentity); 

      MappedDriveResolver mdr = new MappedDriveResolver(); 
      if (mdr.isNetworkDrive(folderPath)) 
      { 
       LoggingAppWrapper.LogDeveloperMessage(folderPath + " is on a Mapped drive", 1, TraceEventType.Information, string.Empty); 

      } 

MappedDriveResolver是一个类,我发现这里How do I determine a mapped drive's actual path?

在该链接的代码工作正常,从一个简单的控制台应用程序,但它是Windows服务的一部分时失败。 有关代码为Windows服务工作需要做什么的建议?

问候。

+0

您确定服务在其中执行的帐户具有适当的权限? – kpcrash 2012-03-29 12:46:55

+0

我想这个答案将帮助您: 【答案线程 “映射网络驱动器被用于服务”] [1] [1]:http://stackoverflow.com/a/4763324/3860297 – sekerg 2014-10-19 12:10:52

回答

4

我建议你配置你的服务,使用UNC路径作为不在运行该服务的服务器上的文件夹。

映射驱动器是用户的可用性功能,因此它们特定于用户配置文件/环境。意思是,当你登录时你可能有一个驱动器X:映射到\\ server1 \ share1,但是当我登录我的驱动器时X:可以被映射到\\ server2 \ share2。实际的映射过程或者以“登录时重新连接”的形式保存为配置文件的一部分,或者由登录脚本处理。

您需要检查哪些账户的服务下运行,确保为用户环境映射驱动器存在(这可能有助于How to map a network drive to be used by a service)。

编辑:

的原因,您的控制台应用程序工程和服务不就是因为环境之间的差异,他们在运行

为了说明这一点,借此控制台应用程序,编译它,然后将其作为计划任务运行。将“路径”变量设置为用户可以访问的映射驱动器。

static void Main(string[] args) { 
     MappedDriveResolver mdr = new MappedDriveResolver(); 
     string logfile; 
     string path = @"I:\"; 
     string[] files; 

     // Write out "log" file to where this is running from 
     logfile = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); 
     logfile = Path.Combine(logfile, "log.txt"); 

     using (StreamWriter sw = new StreamWriter(logfile, true)) { 

      try { 
       sw.WriteLine("Checking path " + path); 
       if (mdr.isNetworkDrive(path)) { 
        sw.WriteLine("Network Drive: Yes"); 
       } else { 
        sw.WriteLine("Network Drive: No"); 
       } 
      } catch (Exception ex) { 
       sw.WriteLine("Exception: " + ex.Message); 
      } 

      try { 
       sw.WriteLine("Resolve path " + path); 
       string newpath = mdr.ResolveToUNC(path); 
       sw.WriteLine("Resolved path " + newpath); 
      } catch (Exception ex) { 
       sw.WriteLine("Exception: " + ex.Message); 
      } 

      try { 
       sw.WriteLine("Get file list from " + path); 
       files = Directory.GetFiles(path); 
       if (files == null || files.Length == 0) { 
        sw.WriteLine("No files found"); 
       } else { 
        sw.WriteLine(string.Format("Found {0} files.", files.Length)); 
       } 
      } catch (Exception ex) { 
       sw.WriteLine("Exception: " + ex.Message); 
      } 

      sw.Flush(); 
      sw.Close(); 
     } 
    } 

注:这是与Windows 7任务计划

测试1:只需通过双击它运行应用程序。
结果是:成功

测试2:成功

测试3:
结果配置计划任务为您的用户帐户与运行“时,登录的用户只能运行”配置计划任务的运行例外

测试4:以“运行用户是否登录或不”
结果用户帐户配置计划任务为“本地服务”帐户运行。
结果:异常

测试1 & 2的工作,因为他们使用的是用户环境,包括映射的驱动器是它的一部分当前登录。

测试3 & 4失败,因为它们为它们创建了自己的用户环境,但没有配置任何映射驱动器。它在那个时候逃脱了我的差异,但是在一些重要的方面,“交互式”和“非交互式”环境有所不同。

+0

好的......我明白了......但为什么代码是从控制台应用程序运行的,而不是从服务运行的? – Codehelp 2012-04-02 05:48:36

+0

我在这里发现了一些很好的信息[http://stackoverflow.com/questions/4396634/how-can-i-determine-if-a-given-drive-letter-is-a-local-mapped-usb-drive] ...再次,这从一个控制台应用程序正常工作,但通过服务失败! – Codehelp 2012-04-02 06:58:19

+0

@Codehelp增加了一个更好的解释和测试方法。 – Vermis 2012-04-02 19:58:12

相关问题