Check if a file is accessable on a shared network drive

10,898

Solution 1

File.Exists() should be fine, but if you start a large copy operation, there's not a lot you can do if the connection goes down during that process, so you'll want to make sure you code for that.

You should trap the IOException and handle it as you see fit.

EDIT: code to trap IOException:

try
{
   File.Copy(myLocalFile, myNetworkFile);
}
catch (IOException ioEx)
{
   Debug.Write(myLocalFile + " failed to copy!  Try again or copy later?");
}

Solution 2

Don't. Just attempt the operation. It will fail just as fast, and you won't be introducing a timing window problem. You have to cope with that failure anyway, why code it twice?

Solution 3

The best idea, of course, would be to create local cache of the setup. You cannot trust network connections. They may slow down or break during operation. If everything is run from network, I would say, it's definitely not a safe idea.

But as far as technical question is concerned, File Exists should be fine. A much more descriptive idea has already been discussed to check the existence of a file. Read here.

FileInfo fi = new FileInfo(@"\\server\share\file.txt");
bool exists = fi.Exists;
Share:
10,898
Data-Base
Author by

Data-Base

Technology is my life, my life is technology :-)

Updated on June 05, 2022

Comments

  • Data-Base
    Data-Base almost 2 years

    I have a program that does different things my questions is related to access files in a network mapped drive or a shared folder

    the program can run a file msi/exe from the network (network mapped drive or a shared folder) the program can copy file from the network (network mapped drive or a shared folder)

    how I can check if the files are accessible before I try to run or copy (in case of a network disconnection, or any other network problem)?

    is it enough with File.Exists();

    here is an example of my code:

    public static bool FileIsOk(string path)
    {
       try
       {
          FileInfo finfo = new FileInfo(path);
    
          if (finfo.Exists)
          {
             return true;
          }
          MessageBox.Show("file does not exist, or there is a problem with the network preventing access to the file!");
          return false;
       }
    
       catch (Exception e)
       {
          MessageBox.Show(e.Message);
       }
       return false;
    }
    

    thanks

    • Tim
      Tim over 13 years
      File.Exists() would be the way to go, unless you need to check certain permissions?
    • Rox
      Rox over 13 years
      Checking for file/folder permissions: stackoverflow.com/questions/3456397/…
  • Data-Base
    Data-Base over 13 years
    the files are small, but can you provide me with an example on how trap the IOException ?
  • Nayan
    Nayan over 13 years
    Well, cache the files means you have to copy the files that you need during the execution to a local temporary folder. If you have a framework that provides copying to local temp folder functionality, use it. If not, create a temp folder, and copy the files from the network location one by one to this folder maintaining the original folder structure. Then use these local files. After the usage is over, delete the local temp folder that you created (unless you have some other use). Read this to understand copying - msdn.microsoft.com/en-us/library/cc148994.aspx