C# 确定路径字符串是本地机器还是远程机器的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/354477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method to determine if path string is local or remote machine
提问by David Boike
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 路径,但仍然是本地的。
采纳答案by Eric Rosenberger
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;
}
}
}
回答by tbetts42
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 地址。
回答by Renato Heeb
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();
}
回答by David
.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));
}
回答by user1471935
Maybe
也许
var isLocal = Dns.GetHostName() == _host || Dns.GetHostEntry(Dns.GetHostName()).AddressList.Any(i => i.ToString().Equals(_host));
回答by Stephen
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);
回答by Art
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;
}