Method to determine if path string is local or remote machine

27,407

Solution 1

Don't know if there's a more efficient way of doing this, but it seems to work for me:

    IPAddress[] host;
    IPAddress[] local;
    bool isLocal = false;

    host = Dns.GetHostAddresses(uri.Host);
    local = Dns.GetHostAddresses(Dns.GetHostName());

    foreach (IPAddress hostAddress in host)
    {
        if (IPAddress.IsLoopback(hostAddress))
        {
            isLocal = true;
            break;
        }
        else
        {
            foreach (IPAddress localAddress in local)
            {
                if (hostAddress.Equals(localAddress))
                {
                    isLocal = true;
                    break;
                }
            }

            if (isLocal)
            {
                break;
            }
        }
    }

Solution 2

This is how I did it.

    public static bool IsLocal(DirectoryInfo dir)
    {
        foreach (DriveInfo d in DriveInfo.GetDrives())
        {
            if (string.Compare(dir.Root.FullName, d.Name, StringComparison.OrdinalIgnoreCase) == 0) //[drweb86] Fix for different case.
            {
                return (d.DriveType != DriveType.Network);
            }
        }
         throw new DriveNotFoundException();
    }

Solution 3

.NET 3.5 version of Eric's answer with an extra check whether the host exists:

    private bool IsLocalHost(string input)
    {
        IPAddress[] host;
        //get host addresses
        try { host = Dns.GetHostAddresses(input); }
        catch (Exception) { return false; }
        //get local adresses
        IPAddress[] local = Dns.GetHostAddresses(Dns.GetHostName()); 
        //check if local
        return host.Any(hostAddress => IPAddress.IsLoopback(hostAddress) || local.Contains(hostAddress));
    }

Solution 4

The following should work for mapped drives and for UNC paths.

private static bool IsLocalPath(String path)
{
    if (!PathIsUNC(path))
    {
        return !PathIsNetworkPath(path);
    }

    Uri uri = new Uri(path);
    return IsLocalHost(uri.Host); // Refer to David's answer
}

[DllImport("Shlwapi.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PathIsNetworkPath(String pszPath);

[DllImport("Shlwapi.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PathIsUNC(String pszPath);

Solution 5

Here is how I addressed a similar need.

        internal static bool IsFileRemote(string path)
    {
            if (String.IsNullOrEmpty(path))
            {
                return false;
            }
            if (new Uri(path).IsUnc)
            {
                return true;
            }
            if (new DriveInfo(path).DriveType == DriveType.Network)
            {
                return true;
            }
            return false;
    }
Share:
27,407
David Boike
Author by

David Boike

Solution Architect at Particular Software Author of Learning NServiceBus, Second Edition NServiceBus Champion & Official Trainer RavenDB Official Trainer Xamarin Certified Mobile Developer Website: http://www.make-awesome.com Twitter: @DavidBoike Husband, father of two, brewer (and drinker) of great craft beer

Updated on January 26, 2020

Comments

  • David Boike
    David Boike over 4 years

    What's the best way, using C# or other .NET language, to determine if a file path string is on the local machine or a remote server?

    It's possible to determine if a path string is UNC using the following:

    new Uri(path).IsUnc
    

    That works great for paths that start with C:\ or other drive letter, but what about paths like:

    \\machinename\sharename\directory
    \\10.12.34.56\sharename\directory
    

    ...where both refer to the local machine - these are UNC paths but are still local.

  • testalino
    testalino over 2 years
    Good answer. It cannot differentiate local network shares in UNC format like asked in the original question. Just something to note.