How to (quickly) check if UNC Path is available

Error processing SSI file

Answers

  1. Joey

    • 2019/9/26

    In my project I use the System.IO :

    if (Directory.Exists(@"\\hostname\samba-sharename\directory")) { ...
    

    and it's pretty fast so far...

  2. Riley

    • 2021/8/19

    How's this for a quick and dirty way to check - run the windows net use command and parse the output for the line with the network path of 

  3. Titus

    • 2021/1/7

    How's this for a quick and dirty way to check - run the windows net use command and parse the output for the line with the network path of interest (e.g. \\vault2) and OK. Here's an example of the output: C:\>net use New connections will be remembered.

  4. Lucca

    • 2015/4/25

    How to check networkshare path working or notI have list of share path I want to display that share path is working or accessible or not So far i google it 

  5. Gianni

    • 2017/3/16

    I used the ping method suggested above and it did not work for me since I am using OpenDNS Here is a function that worked well for me:

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    /// <summary>
    /// A quick method to test is the path exists 
    /// </summary>
    /// <param name="s"></param>
    /// <param name="timeOutMs"></param>
    /// <returns></returns>
    public static bool CheckPathExists(string s, int timeOutMs = 120) {
        if (s.StartsWith(@"\\")) {
            Uri uri = new Uri(s);
            if (uri.Segments.Length == 0 || string.IsNullOrWhiteSpace(uri.Host))
                return false;
            if (uri.Host != Dns.GetHostName()) {
                WebRequest request;
                WebResponse response;
                request = WebRequest.Create(uri);
                request.Method = "HEAD";
                request.Timeout = timeOutMs;
                try {
                    response = request.GetResponse();
                } catch (Exception ex) {
                    return false;
                }
                return response.ContentLength > 0;
    
                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
                // Do a Ping to see if the server is there
                // This method doesn't work well using OPenDNS since it always succeeds
                // regardless if the IP is a valid or not
                // OpenDns always maps every host to an IP. If the host is not valid the 
                // OpenDNS will map it to 67.215.65.132
                /* Example:
                    C:\>ping xxx
    
                    Pinging xxx.RT-AC66R [67.215.65.132] with 32 bytes of data:
                    Reply from 67.215.65.132: bytes=32 time=24ms TTL=55
                    */
    
                //Ping pingSender = new Ping();
                //PingOptions options = new PingOptions();
                // Use the default Ttl value which is 128,
                // but change the fragmentation behavior.
                //options.DontFragment = true;
    
                // Create a buffer of 32 bytes of data to be transmitted.
                //string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                //byte[] buffer = Encoding.ASCII.GetBytes(data);
                //int timeout = 120;
                //PingReply reply = pingSender.Send(uri.Host, timeout, buffer, options);
                //if (reply == null || reply.Status != IPStatus.Success)
                //    return false;
            }
        }
        return File.Exists(s);
    }
    
  6. Milo

    • 2019/1/4

    What is the correct way to check if a path is an UNC path or a local , Since a path without two backslashes in the first and second positions is, by definiton, not a UNC path, this is a safe way to make this Uri uncPath = new Uri(@"\\my\unc\path"); Console.WriteLine(uncPath.IsUnc); Furthermore you can see how this is implemented in the reference source.

  7. Messiah

    • 2019/12/30

    I'd have to see more of the script to say the best way to do it though. Reply. Was this post helpful? DPiddy 

  8. Zaire

    • 2020/6/13

    How to check networkshare path working or not. I have list of share path. I want to display that share path is working or accessible or not. So far i google it, I am getting

  9. Moises

    • 2020/5/31

    I ended up "cheating" and just pinging the host, which is reasonable as that in reality is the case that I'm checking for.

    private bool HostExists(string PCName)
    {
        Ping pinger = new Ping();
    
        try
        {
            PingReply reply = pinger.Send(PCName);
            return reply.Status == IPStatus.Success;
        }
        catch
        {
            return false;
        }
        finally
        {
            pinger.Dispose();
        }
    
    }
    

    This way suits me best because of its speed, simplicity, and reliability.

  10. Finnley

    • 2015/10/31

    usual way to check if file exists. DECLARE @File_Exists INT EXEC Master.dbo.xp_fileexist '\\serverB\SQL_Backup\file.bak', @File_Exists OUT

  11. Jayceon

    • 2017/12/24

    In Windows, if you have mapped network drives and you don't know the UNC path for them, you can start a command prompt (Start → Run → cmd.exe) and use the net use command to list your mapped drives and their UNC paths: C:\>net use New connections will be remembered.

  12. Raul

    • 2020/12/19

    I have written an application that uses Directory.Exists to determine whether a series of network paths is available and output the results 

  13. Francisco

    • 2018/8/19

    In a project of mine, i had to check whether a server connection was established or not. I used a TCP Socket to asynchronically check whether the server could be reached or not. I wonder if you could use this to check on a network share. The async TCP Socket connect goes so fast i tested for the connection 10 times under 60 miliseconds. Maybe you could play around with that a bit ?


    EDIT: Here is the Asynchronous Socket i used for my project. Use this class to check for a certain IP or address. Hope it is of any use to you

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;
    
    namespace Base.BaseObjects
    {
        public class AsynchronousClient
        {
            #region Properties
    
            private int _port = 0000, currentTry = 0, _buffersize, _fastpingdelay = 80;
            private string _server = "localhost";
            private Socket client;
            private static IPEndPoint remoteEP;
    
            // Delegates & Events
            public delegate void SendMessageDelegate(string message);
            public event SendMessageDelegate SendMessageEvent;
            public delegate void ConnectionStatusDelegate(bool connected, bool reconnect);
            public event ConnectionStatusDelegate ConnectionStatusChanged;
    
            // ManualResetEvent instances signal completion.
            private static ManualResetEvent connectDone = new ManualResetEvent(false);
            private static ManualResetEvent sendDone = new ManualResetEvent(false);
            private static ManualResetEvent receiveDone = new ManualResetEvent(false);
    
            /// <summary>
            /// Port to monitor
            /// </summary>
            public int Port { get { return _port; } }
    
            /// <summary>
            /// Number of packages to buffer until system reports connection loss
            /// </summary>
            public int BufferSize { get { return _buffersize; }  }
    
            /// <summary>
            /// Time in milliseconds between two pings
            /// </summary>
            public int FastPingDelay { get { return _fastpingdelay; } }
    
            /// <summary>
            /// Servername to connect to
            /// </summary>
            public string Server
            {
                get { return _server; }
                set
                {
                    _server = value;
                    // Resolve the remote endpoint for the socket.
                    try
                    {
                        IPAddress ipAddress = (IPAddress)Dns.GetHostAddresses(value)[0];
                        remoteEP = new IPEndPoint(ipAddress, Port);
                    }
                    catch (SocketException ex)
                    {
                        SendMessage(ex.Message);
                    }
                }
            }
    
            #endregion
    
            #region Events & Delegates
    
            protected void SendMessage(string message)
            {
                if (SendMessageEvent != null)
                    SendMessageEvent(message);
            }
    
            protected void UpdateConnectionStatus(bool connected, bool reconnect)
            {
                if (ConnectionStatusChanged != null)
                    ConnectionStatusChanged(connected, reconnect);
            }
    
            private void ConnectCallback(IAsyncResult ar)
            {
                try
                {
                    // Retrieve the socket from the state object.
                    Socket client = (Socket)ar.AsyncState;
    
                    // Complete the connection.
                    client.EndConnect(ar);
    
                    SendMessage(String.Format("Socket connected to {0}", client.RemoteEndPoint.ToString()));
                    //UpdateConnectionStatus(true, false);
    
                    // Signal that the connection has been made.
                    connectDone.Set();
                }
                catch (Exception e)
                {
                    SendMessage(e.ToString());
                    UpdateConnectionStatus(false, true);
                }
            }
    
            #endregion
    
            #region methods
    
            public AsynchronousClient(int port, string server)
            {
                _port = port;
                Server = server;
                _buffersize = 10;
                _fastpingdelay = 20;
            }
    
            public void CreateSocket()
            {
                try
                {
                    StopClient();
                }
                catch (Exception ex)
                {
                    SendMessage(ex.Message);
                }
                finally
                {
                    client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                }
            }
    
            public bool FastPingSocket()
            {
                for (currentTry = 0; currentTry <= BufferSize; currentTry++)
                {
                    try
                    {
                        CreateSocket();
                        client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
                        connectDone.WaitOne();
                        System.Threading.Thread.Sleep(FastPingDelay);
                        client.Shutdown(SocketShutdown.Receive);
                        connectDone.WaitOne();
                        client.Close();
                        return true;
                    }
                    catch (SocketException ex)
                    {
                        SendMessage(ex.Message);
                    }
                    catch (ObjectDisposedException ex)
                    {
                        currentTry--;
                        SendMessage(ex.Message);
                        CreateSocket();
                    }
                    catch (NullReferenceException ex)
                    {
                        currentTry--;
                        SendMessage(ex.Message);
                        CreateSocket();
                    }
                    catch (ArgumentNullException ex)
                    {
                        SendMessage(ex.Message);
                        CreateSocket();
                    }
                    catch (InvalidOperationException ex)
                    {
                        SendMessage(ex.Message);
                        CreateSocket();
                        currentTry--;
                    }
                    finally
                    {
                        StopClient();
                    }
                }
                UpdateConnectionStatus(false, true);
                return false;
            }
    
            public void StopClient()
            {
                // Release the socket.
                try
                {
                    client.Shutdown(SocketShutdown.Both);
                    client.Close();
                }
                catch (Exception) { }
                finally
                {
                    UpdateConnectionStatus(false, false);
                }
            }
    
            #endregion
    
        }
    }
    
    

    Edit: Please don't just copy/paste it. Try to understand the code so you can use it for your benefit, and finetune it for your needs.

  14. Tripp

    • 2016/7/27

    var networkPath = String.Empty; // If this is a rooted path, just make sure it is available. if (Path.IsPathRooted(serverName)) { // If the path exists, use it. if (Directory.Exists(serverName)) networkPath = serverName; } // Else this is a network path.

  15. Ty

    • 2016/8/12

    I have tried DirExists (path) and FileExists(path) and fso objects. They all work great when the path is available. My problem is in when the application 

  16. Maverick

    • 2017/9/26

    VBA code to check if a shareDrive is available I would like to write a code that would check to is if the computer is connected to the UNC Path from a Mapped Network Share's Drive. If it is not, I want to display a massage telling the user they are not connected to the folder, and if it is to run some other code.

  17. Sutton

    • 2020/1/8

    How's this for a quick and dirty way to check - run the windows net use command and parse the output for the line with the network path of interest (e.g. \\vault2) and OK. Here's an example of the output:

    C:\>net use
    New connections will be remembered.
    
    Status       Local     Remote                    Network
    
    -------------------------------------------------------------------------------
    OK           O:        \\smarty\Data       Microsoft Windows Network
    Disconnected P:        \\dummy\Data       Microsoft Windows Network
    OK                     \\vault2\vault2           Microsoft Windows Network
    The command completed successfully.
    

    It's not a very .netish solution, but it's very fast, and sometimes that matters more :-).

    And here's the code to do it (and LINQPad tells me that it only takes 150ms, so that's nice)

    void Main()
    {
        bool available = QuickBestGuessAboutAccessibilityOfNetworkPath(@"\\vault2\vault2\dir1\dir2");
        Console.WriteLine(available);
    }
    
    public static bool QuickBestGuessAboutAccessibilityOfNetworkPath(string path)
    {
        if (string.IsNullOrEmpty(path)) return false;
        string pathRoot = Path.GetPathRoot(path);
        if (string.IsNullOrEmpty(pathRoot)) return false;
        ProcessStartInfo pinfo = new ProcessStartInfo("net", "use");
        pinfo.CreateNoWindow = true;
        pinfo.RedirectStandardOutput = true;
        pinfo.UseShellExecute = false;
        string output;
        using (Process p = Process.Start(pinfo)) {
            output = p.StandardOutput.ReadToEnd();
        }
        foreach (string line in output.Split('\n'))
        {
            if (line.Contains(pathRoot) && line.Contains("OK"))
            {
                return true; // shareIsProbablyConnected
            }
        }
        return false;
    }
    

    Or you could probably go the route of using WMI, as alluded to in this answer to How to ensure network drives are connected for an application?

  18. Alijah

    • 2017/4/7

    You can use the exist command to check if the path is valid: if exist \\192.168.1.1\drive1 net use s: \\192.168.1.1\drive1. If you need to provide 

  19. Rinaldi

    • 2018/2/9

    Hi, first of all, please check the sharing settings, make sure the account have permission to access. Then try to use IP address access to the sharing file for test. If failed, this can be DNS problem. On the other hand, please also check NTFS Permission, make sure there is no problem with it.

  20. Damian

    • 2018/8/25

    My specific problem is that I want to create an System.IO.DriveInfo-object if there is a drive letter in the path. .net path unc · Source.

  21. Martin

    • 2018/12/28

    We use cookies on our website to provide you with the best experience. If you continue browsing, you are consenting to our use of these cookies, but if you 

  22. Martini

    • 2021/3/29

    How to use a Universal Naming Convention (UNC) file path instead of may not be able to determine immediately if the mapped drive exists.

Comments are closed.

More Posts