Application says network drive doesn't exist, but found using OpenFileDialog
Asked Answered
R

5

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?

Ruzich answered 6/7, 2012 at 4:51 Comment(8)
How about you pass in the \\server\sharedFolder instead of mapped network driver Y:? Does it have the same problem?Eastwood
Also, try putting some retry logic around fn_drive_exists (perhaps loop 5 times with 6 second delay per loop).Brindisi
Sounds like UNC path will work in your case. See this SO postEastwood
@Harvey: How do I do exactly? I believe I should write something else instead of "Server" and "sharedfolder" ? An IP-address?Ruzich
@Eric: Thank you for the suggestion. I tried, but that didn't solve it.Ruzich
@Harvey: Thanks! I got it working! Using UNC is the way! And thanks to this: 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.Ruzich
Could you please post your solution as an answer? Otherwise this question will keep being on the list of unanswered questions.Amplification
I have made good use of this example before: codeproject.com/Articles/90143/Mapping-Network-Drive-using-CMakedamakefast
E
1

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

Eastwood answered 7/7, 2012 at 19:34 Comment(0)
P
0

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

Pedology answered 23/7, 2012 at 3:51 Comment(0)
I
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.

Interpretation answered 11/8, 2012 at 6:1 Comment(0)
W
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>)

Words answered 15/8, 2012 at 20:35 Comment(0)
A
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

Arequipa answered 15/9, 2012 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.