从使用C#编写的Windows服务访问映射文件夹

时间:2012-03-29 12:43:32

标签: c#

我有一个Windows服务,它会轮询特定文件夹以创建新文件。当文件夹位于其中一个本地驱动器(如C:或D)时,此工作正常: 该服务无法在映射的驱动器上找到文件夹。

以下是在轮询之前检查文件夹的代码:

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服务工作的代码需要做些什么的任何建议?

问候。

1 个答案:

答案 0 :(得分:4)

我建议您将服务配置为对运行该服务的服务器上的文件夹使用UNC路径。

映射驱动器是用户的可用性功能,因此它们特定于该用户配置文件/环境。这意味着,当您登录时,您可能有一个驱动器X:映射到\\ server1 \ share1但是当我登录我的驱动器时X:可以映射到\\ server2 \ share2而不是。实际的映射过程可以通过“登录时重新连接”保存为配置文件的一部分,也可以通过登录脚本进行处理。

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

修改

您的控制台应用程序正常工作而服务不正常的原因是它们运行的​​环境之间存在差异。

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

    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失败,因为他们为自己创建了自己的用户环境,没有配置任何映射驱动器。目前它让我感到不同的是,但是“交互式”和“非交互式”环境在某些重要方面是不同的。