Check if the file is accessible on a shared network drive

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

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

How can I check if the files are available before I try to start or copy (in case of a network outage or any other network problem)?

is 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;
}

thank

+3
source share
3 answers

.Exists() , , , , , .

IOException .

EDIT: IOException:

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

. . , . , ?

+3

, , . . . , , .

But on a technical issue, File Exists should be fine. A more detailed descriptive idea has already been discussed to verify the existence of the file. Read here .

FileInfo fi = new FileInfo(@"\\server\share\file.txt");
bool exists = fi.Exists;
+1
source

Source: https://habr.com/ru/post/1768225/


All Articles