12

I have made a little app that's running on a Win7-PC. All it does, is to check the content of a network drive at 1:00 O'clock in the morning (and compare it to a folder on its local hard drive), and if there´s differences, copy the differences to this folder.

The problem is, sometimes it can not find the network drive.

When the app starts up, the network drive is found using a button on the app which starts OpenFileDialog, and the resulting drive letter is put into a textbox beside the button. From that point it should just run by itself. The PC is never turned off.

When it says the network drive can not be found, I can manually press the button on the very same app, select the drive in the OpenFileDialog (the drive letter never changes), and the app will run flawless in a couple of days. Then the problem occurs again.

The question is: Why can the network drive be accessed through the OpenFileDialog on my app, but my app can not?

My app start the copy-process using this function (called with "Y:\") to determine whether the drive is present or not:

    public bool fn_drive_exists(string par_string)
    {
        DirectoryInfo di_dir = new DirectoryInfo(par_string);
        if (di_dir.Exists)
        {
            return true;
        }

        return false;
    }

...and sometimes it returns a False, until I "wake it up" using the OpenFileDialog.

What does OpenFileDialog do, that my app do not?

8
  • 3
    How about you pass in the \\server\sharedFolder instead of mapped network driver Y:? Does it have the same problem? Jul 6, 2012 at 4:56
  • Also, try putting some retry logic around fn_drive_exists (perhaps loop 5 times with 6 second delay per loop).
    – Eric J.
    Jul 6, 2012 at 4:59
  • 1
    Sounds like UNC path will work in your case. See this SO post Jul 6, 2012 at 5:07
  • @Harvey: How do I do exactly? I believe I should write something else instead of "Server" and "sharedfolder" ? An IP-address? Jul 6, 2012 at 5:32
  • @Eric: Thank you for the suggestion. I tried, but that didn't solve it. Jul 6, 2012 at 5:37

5 Answers 5

1

According to this SO post, the problem should be gone if you use UNC path instead of mapped network drive.

0

If your destination has a static ip address, I suggest you use that ip address instead of domain name for network drive

0

This SO post describes a similar scenario to what you've described.

One of the links posted as a response to that question led me to this MSDN article which provides a variety of reasons as to why one might encounter errors when trying to access shared network drives by using a mapped drive letter.

Microsoft's suggestion (see below) is to simply use a UNC path.

A service (or any process running in a different security context) that must access a remote resource should use the Universal Naming Convention (UNC) name to access the resource.

To answer your actual question more specifically, with regards to why it suddenly can't access the network share, I would venture a guess to say that the network share is being disconnected by Windows due to an idle timeout, as discussed in KB297684. Any attempt to access the disconnected drive will be met with a small wait as the connection to the network share is re-established, which could presumably be what is causing your issue.

To test this theory, try writing some data to a file on the network drive at a relatively short interval (every 10 minutes, perhaps?) to try and convince Windows that the drive is still active.

0

You can also try to use:

System.IO.Directory.Exists(par_string);

instead of writing Your own method for the same thing. I would expect a framework method to be able to "wake" the network drive. Note: Method also works for UNC paths (something like \\<server name or IP address>\<shared folder>)

0

Like Harvey says, use the UNC path to access the folder, for instance \\server\sharedfolder. In place of \\server use the name of the server. Your computer has a name and so does the server. You can also use the IP address if you know it. You replace \sharedfolder with the path to the files. Some examples:

\\AppsServer\c$\Program Files(x86)

\\FileServer1\d$\Users\John\My Documents

The c$ represents that the C drive is the shared folder. If the entire drive is not shared, you will need to share the specific folder. You can do that by logging onto the server, right clicking the folder, and selecting Properties. Then you go to the Sharing tab and check the Share this folder checkbox. If your shared folder is called MyShare, then your UNC path to access the folder will be

\\server\MyShare

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.