简体   繁体   中英

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

I'm working on file replication service in C#. The service works perfectly in an environment where I have access to the user space; however, when I run it as a service I start encountering errors.

There is a lot of information out on accessing UNC shares in this scenario, but after pursuing what seemed to be the most likely solution, I've still come up short.

In my 'faulty' environment, the service is running as the 'administrator' account, and I've taken a couple of approaches; both using a mapped network drive, and a specific UNC share, and end up with the same outcome in both circumstances.

My constructor contains the logic for detection of whether or not the file exists, so it should be the only relevant piece in this equation;

    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 + ".");
                }
            }
        }
    }

The only exception to my last statement is the potential necessity of knowing what my UNCHandler class does - but to quell that storm, it is an exact rip from the answer Found Here

To be clear - the issue here is that the File.Exists and Directory.Exists checks fail, even after an attempt to connect to the remote system.

My error log hands me back the following; 'system|ReadConfiguration:Cannot instantiate FileMonitor for file that does not exist at Z:.' - which is effectively the exception that I generate in the above constructor.

I've tried using a variety of methods to reach my 'source'; including using a UNC share and a mapped drive, only to yield no difference in results.

I took an answers advice and ran microsoft's Process Monitor in an attempt to look further into this, but have yet to find any information in this venue that will help me. Under my process, I get dozens of SUCCESS's until I attempt to reach the share - at which point the only indicative results are a 'NAME NOT FOUND' against a CreateFile operation, and a 'FILE LOCKED WITH ONLY READERS' moments later against a 'CreateFileMapping' call.

The process is running as the local systems Administrator account, and in my 'user space' I have a mapped drive to the same location I am trying to reach, that I can manipulate fully.

What error are you getting? Have you tried running ProcessMonitor ( http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx ) to see what is happening? Are you running as the local administrator for that computer? Does that local administrator account actually have access to the share in question? Have you tried using psexec ( http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx ) to run a command shell as that user and see if you actually have access to that share?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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