确定路径字符串是本地计算机还是远程计算机的方法

时间:2008-12-09 22:26:20

标签: c# .net uri unc

使用C#或其他.NET语言确定文件路径字符串是在本地计算机还是远程服务器上的最佳方法是什么?

可以使用以下内容确定路径字符串是否为UNC:

new Uri(path).IsUnc

这对于以C:\或其他驱动器号开头的路径非常有用,但是路径如下:

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

...两者都引用本地机器 - 这些是UNC路径,但仍然是本地路径。

7 个答案:

答案 0 :(得分:21)

这就是我做到的。

    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();
    }

答案 1 :(得分:17)

不知道是否有更有效的方法,但它似乎对我有用:

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

答案 2 :(得分:12)

.NET 3.5版本的Eric的答案,额外检查主机是否存在:

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

答案 3 :(得分:7)

以下内容适用于映射驱动器和UNC路径。

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);

答案 4 :(得分:5)

以下是我解决类似需求的方法。

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

答案 5 :(得分:0)

我不知道检查它的单一方法。但是,您可以将Uri的Host属性与本地主机名或IP地址进行比较。

您可以使用以下方式获取本地计算机名称:

string hostName = System.Net.Dns.GetHostName()

然后,您可以通过将该字符串传递给:

来获取IP地址数组
System.Net.IPAddress[] addresses = System.Net.Dns.GetHostAddresses(hostName);

启用Uri的HostNameType属性,可能是UriHostNameType.DnsUriHostNameType.IPv4,以匹配名称或IP地址。

答案 6 :(得分:0)

也许

var isLocal = Dns.GetHostName() == _host || Dns.GetHostEntry(Dns.GetHostName()).AddressList.Any(i => i.ToString().Equals(_host));