【问题标题】:Method to determine if path string is local or remote machine确定路径字符串是本地机器还是远程机器的方法
【发布时间】:2010-09-26 03:37:21
【问题描述】:

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

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

new Uri(path).IsUnc

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

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

...都指本地机器 - 这些是 UNC 路径,但仍然是本地的。

【问题讨论】:

    标签: c# .net uri unc


    【解决方案1】:

    我就是这样做的。

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

    【讨论】:

    • 使用,抛出新的 System.IO.DriveNotFoundException();
    【解决方案2】:

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

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

    【讨论】:

    • 如果您正在查找的 uri 已关闭或未连接(或您自己的 PC 未连接),也不起作用。
    • 这不是一个完整的解决方案
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      以下内容应该适用于映射驱动器和 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);
      

      【讨论】:

        【解决方案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;
            }
        

        【讨论】:

        • 好答案。它无法像原始问题中所问的那样区分 UNC 格式的本地网络共享。只是需要注意的一点。
        【解决方案6】:

        也许

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

        【讨论】:

          【解决方案7】:

          我不知道有什么方法可以检查。但是,您可以将 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 地址。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-12-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-24
            • 2011-08-24
            • 1970-01-01
            相关资源
            最近更新 更多