方法来确定路径字符串是本地或远程计算机 [英] Method to determine if path string is local or remote machine
本文介绍了方法来确定路径字符串是本地或远程计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是最好的方式,使用C#或其他.NET语言,以确定是否一个文件路径字符串是本地计算机或远程服务器上?
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?
这是可能的,以确定是否一个路径字符串是UNC使用以下:
It's possible to determine if a path string is UNC using the following:
new Uri(path).IsUnc
这是该开始用C道路的伟大工程:关于类似的路径\或其他驱动器盘符,但什么:
That works great for paths that start with C:\ or other drive letter, but what about paths like:
\\machinename\sharename\directory
\\10.12.34.56\sharename\directory
...在这里指的是本地机器 - 这些是UNC路径,但仍有局部
...where both refer to the local machine - these are UNC paths but are still local.
推荐答案
不知道是否有这样做的更有效的方式,但它似乎为我工作:
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;
}
}
}
这篇关于方法来确定路径字符串是本地或远程计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文