繁体   English   中英

为什么我不能从Windows服务中找到此UNC路径?

[英]Why can't I hit this UNC path from my Windows Service?

我正在使用C#开发文件复制服务。 该服务在我可以访问用户空间的环境中运行良好; 但是,当我将其作为服务运行时,我开始遇到错误。

在这种情况下,有关访问UNC共享的信息很多,但是在寻求似乎最有可能的解决方案之后,我仍然很简短。

在我的“故障”环境中,该服务以“管理员”帐户的身份运行,并且我采取了几种方法。 两者都使用映射的网络驱动器和特定的UNC共享,并且在两种情况下最终都具有相同的结果。

我的构造函数包含用于检测文件是否存在的逻辑,因此它应该是该方程式中唯一相关的部分。

    public FileMonitor(String TargetPath)
        : base()
    {
        if (String.IsNullOrEmpty(TargetPath))
        {
            throw new ArgumentNullException("Cannot instantiate FilesystemMonitor. TargetPath was not provided or is null.");
        }
        else
        {
            this.FileCache = new Dictionary<string, DateTime>();

            if (Directory.Exists(TargetPath))
            {
                this.TargetDirectory = new DirectoryInfo(TargetPath);
                return;
            }
            else if (File.Exists(TargetPath))
            {
                this.TargetFile = new FileInfo(TargetPath);
                return;
            }
            else
            {
                if (TargetPath.StartsWith("\\\\"))
                {
                    FileInfo Finfo = new FileInfo(TargetPath);

                    UNCHandler.connectToRemote(Finfo.DirectoryName, "administrator", "password");

                    if (Directory.Exists(TargetPath))
                    {
                        this.TargetDirectory = new DirectoryInfo(TargetPath);
                        return;
                    }
                    else if (File.Exists(TargetPath))
                    {
                        this.TargetFile = new FileInfo(TargetPath);
                        return;
                    }
                    else
                    {
                        throw new InvalidOperationException("Cannot instantiate FileMonitor for file that does not exist at " + TargetPath + ".");
                    }
                }
                else
                {
                    throw new InvalidOperationException("Cannot instantiate FileMonitor for file that does not exist at " + TargetPath + ".");
                }
            }
        }
    }

我上一条语句的唯一例外是可能有必要知道UNCHandler类的功能-但要平息这场风暴,这是从“ 找到这里”的答案中得到的确切答案。

需要明确的是,这里的问题是即使尝试连接到远程系统后,File.Exists和Directory.Exists检查也会失败。

我的错误日志让我退回了以下内容; 'system | ReadConfiguration:无法实例化Z:处不存在的文件的FileMonitor。' -这实际上是我在上述构造函数中生成的异常。

我尝试使用各种方法来达到我的“来源”。 包括使用UNC共享和映射的驱动器,仅使结果无差异。

我接受了一个答案建议,并运行了Microsoft的Process Monitor,试图对此做进一步的研究,但还没有在该场所找到任何对我有帮助的信息。 在我的过程中,我获得了数十次成功,直到尝试达到份额为止。在这一点上,唯一的指示性结果是针对CreateFile操作的“名称未找到”,以及稍后针对“文件未锁定的文件”。 CreateFileMapping”调用。

该进程以本地系统管理员帐户的身份运行,在我的“用户空间”中,我有一个映射的驱动器到我要访问的相同位置,可以完全操作。

你遇到了什么错误? 您是否尝试过运行ProcessMonitor( http://technet.microsoft.com/zh-cn/sysinternals/bb896645.aspx )以查看发生了什么情况? 您是否以该计算机的本地管理员身份运行? 该本地管理员帐户实际上是否有权访问所讨论的共享? 您是否尝试过使用psexec( http://technet.microsoft.com/zh-cn/sysinternals/bb897553.aspx )以该用户身份运行命令外壳,并查看您是否实际上有权访问该共享?

暂无
暂无

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

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