[英]Method to determine if path string is local or remote machine
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? 使用C#或其他.NET语言确定文件路径字符串是在本地计算机还是远程服务器上的最佳方法是什么?
It's possible to determine if a path string is UNC using the following: 可以使用以下内容确定路径字符串是否为UNC:
new Uri(path).IsUnc
That works great for paths that start with C:\\ or other drive letter, but what about paths like: 这适用于以C:\\或其他驱动器号开头的路径,但是路径如下:
\\machinename\sharename\directory
\\10.12.34.56\sharename\directory
...where both refer to the local machine - these are UNC paths but are still local. ...两者都引用本地机器 - 这些是UNC路径但仍然是本地路径。
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();
}
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;
}
}
}
.NET 3.5 version of Eric's answer with an extra check whether the host exists: .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));
}
The following should work for mapped drives and for UNC paths. 以下内容适用于映射驱动器和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);
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;
}
也许
var isLocal = Dns.GetHostName() == _host || Dns.GetHostEntry(Dns.GetHostName()).AddressList.Any(i => i.ToString().Equals(_host));
I don't know of a single method to check that. 我不知道有哪种方法可以检查。 However, you can compare the Uri's
Host
property to either the local host name or IP address. 但是,您可以将Uri的
Host
属性与本地主机名或IP地址进行比较。
You can get the local machine name using: 您可以使用以下方式获取本地计算机名称
string hostName = System.Net.Dns.GetHostName()
You can then get an array of IP addresses by passing that string to: 然后,您可以通过将该字符串传递到以下内容来获取IP地址数组:
System.Net.IPAddress[] addresses = System.Net.Dns.GetHostAddresses(hostName);
Switch on the Uri's HostNameType
property, probably UriHostNameType.Dns
or UriHostNameType.IPv4
, to match either the name or IP address. 打开Uri的
HostNameType
属性,可能是UriHostNameType.Dns
或UriHostNameType.IPv4
,以匹配名称或IP地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.