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?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Mads Aggerholm
  • 432
  • 1
  • 5
  • 13
  • 3
    How about you pass in the `\\server\sharedFolder` instead of mapped network driver `Y:`? Does it have the same problem? – Harvey Kwok Jul 06 '12 at 04:56
  • Also, try putting some retry logic around fn_drive_exists (perhaps loop 5 times with 6 second delay per loop). – Eric J. Jul 06 '12 at 04:59
  • 1
    Sounds like UNC path will work in your case. See this [SO post](http://stackoverflow.com/questions/133660/how-can-i-access-a-mapped-network-drive-with-system-io-directoryinfo) – Harvey Kwok Jul 06 '12 at 05:07
  • @Harvey: How do I do exactly? I believe I should write something else instead of "Server" and "sharedfolder" ? An IP-address? – Mads Aggerholm Jul 06 '12 at 05:32
  • @Eric: Thank you for the suggestion. I tried, but that didn't solve it. – Mads Aggerholm Jul 06 '12 at 05:37
  • @Harvey: Thanks! I got it working! Using UNC is the way! And thanks to this: http://www.wiredprairie.us/blog/index.php/archives/22 I was able to make a system where the user can use the folderbrowserdialog to select a drive, and it´ll automatically work for local drives and network-drives. – Mads Aggerholm Jul 06 '12 at 08:30
  • Could you please post your solution as an answer? Otherwise this question will keep being on the list of unanswered questions. – Gorgsenegger Jul 07 '12 at 06:08
  • I have made good use of this example before: http://www.codeproject.com/Articles/90143/Mapping-Network-Drive-using-C –  Sep 11 '12 at 14:19

5 Answers5

1

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

Community
  • 1
  • 1
Harvey Kwok
  • 11,713
  • 6
  • 37
  • 59
0

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

Fabio
  • 1
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.

Community
  • 1
  • 1
Joshua Shearer
  • 1,120
  • 10
  • 23
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>)

Matt Stuvysant
  • 439
  • 4
  • 6
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

John C
  • 880
  • 10
  • 13