【问题标题】:Accessing mapped folder from a Windows Service written in C#从用 C# 编写的 Windows 服务访问映射文件夹
【发布时间】:2012-04-13 02:41:01
【问题描述】:

我有一个 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] [1]:stackoverflow.com/a/4763324/3860297

标签: c#


【解决方案1】:

我建议您将服务配置为对不在运行该服务的服务器上的文件夹使用 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 失败,因为它们为它们创建了自己的用户环境,但没有配置任何映射驱动器。目前我不知道有什么区别,但是“交互式”和“非交互式”环境在某些重要方面是不同的。

【讨论】:

  • 好的……我明白了……但是为什么代码可以从控制台应用程序而不是服务中工作?
  • 我在这里找到了一些很好的信息 [stackoverflow.com/questions/4396634/… ... 再次,这在控制台应用程序中运行良好,但通过服务失败!!!
  • @Codehelp 添加了一个有希望的更好的解释和测试它的方法。
猜你喜欢
  • 1970-01-01
  • 2012-10-22
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 2012-04-10
  • 1970-01-01
相关资源
最近更新 更多