简体   繁体   中英

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

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?

根据这篇SO帖子 ,如果您使用UNC路径而不是映射网络驱动器,问题应该消失。

如果您的目的地有静态IP地址,我建议您使用该IP地址而不是域名进行网络驱动

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.

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> )

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

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