JG Vimalan's Blog

It is all about coding!

Ping UNC path

Hi, 

The code Directory.Exists(“uncPath”); is too slow for unavailable/disconnected unc path. So, to solve this issue, we can ping the unc path before checking for the directory. The following code snippet will give you an idea of pinging unc path, 

       public static bool PingUNCPath(string uncPath)
       {
           string hostNameOrAddress = string.Empty;
           string[] uncPathMembers = uncPath.Split(‘\\’);

           //You may have a path some thing like \\\\abcd001\\Documents\\New\\

          //We need only the computer name. So split the path.

           //Get the host name or address.
           foreach (string pathName in uncPathMembers)
           {
               if (!string.IsNullOrEmpty(pathName))
               {
                   //first non empty word is host name
                   hostNameOrAddress = pathName;
                   break;
               }
           }
          
           try
           {
               System.Net.NetworkInformation.Ping ping = new  System.Net.NetworkInformation.Ping();
               System.Net.NetworkInformation.PingReply reply = ping.Send (hostNameOrAddress, 200); //200ms

               if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
                   return true;
           }
           catch //if host name is not found, you get ping exception!!!
           {
               //Invalid Operation
               //Not supported
               //Ping exception
               //Socket exception
               //object disposed exception
               return false;
           }          

           return false;
       } 

Regards,

JG Vimalan

Advertisements

August 10, 2009 - Posted by | C#.NET

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: