2008-12-09 50 views
30

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

有可能确定一个路径字符串是UNC使用下列内容:

new Uri(path).IsUnc 

,对于以C开始路径的伟大工程:关于像路径\或其他驱动器盘符,但什么:

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

...其中两个引用本地机器 - 这些是UNC路径,但仍然是本地的。

回答

15

不知道是否有这样做的更有效的方式,但它似乎为我工作:

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

不适用于映射驱动器。 – 2011-11-14 12:19:17

+1

如果您正在查找的uri被关闭或未连接(或您自己的PC未连接),也不起作用。 – midspace 2011-12-18 23:49:43

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地址。

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

也许

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

下应该映射驱动器和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; 
    } 
相关问题