Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法确定路径字符串是本地计算机还是远程计算机_C#_.net_Uri_Unc - Fatal编程技术网

C# 方法确定路径字符串是本地计算机还是远程计算机

C# 方法确定路径字符串是本地计算机还是远程计算机,c#,.net,uri,unc,C#,.net,Uri,Unc,使用C#或其他.NET语言确定文件路径字符串是在本地计算机上还是在远程服务器上的最佳方法是什么 可以使用以下方法确定路径字符串是否为UNC: new Uri(path).IsUnc 这对于以C:\或其他驱动器号开头的路径非常有效,但对于以下路径又如何呢: \\machinename\sharename\directory \\10.12.34.56\sharename\directory …其中两者都指向本地计算机-这些是UNC路径,但仍然是本地路径。我不知道有哪种方法可以检查这一点。但是,

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

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

new Uri(path).IsUnc
这对于以C:\或其他驱动器号开头的路径非常有效,但对于以下路径又如何呢:

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

…其中两者都指向本地计算机-这些是UNC路径,但仍然是本地路径。

我不知道有哪种方法可以检查这一点。但是,您可以将Uri的
Host
属性与本地主机名或IP地址进行比较

您可以使用以下方法获取本地计算机名:

string hostName = System.Net.Dns.GetHostName()
然后,您可以通过将该字符串传递给以下对象来获取IP地址数组:

System.Net.IPAddress[] addresses = System.Net.Dns.GetHostAddresses(hostName);

打开Uri的
HostNameType
属性,可能是
UriHostNameType.Dns
UriHostNameType.IPv4
,以匹配名称或IP地址。

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

    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;
            }
        }
    }
我就是这样做的

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

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


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

下面是我如何解决类似需求的

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

如果您正在查找的uri已关闭或未连接(或您自己的电脑未连接),也不起作用。这不是一个完整的解决方案使用时,抛出new System.IO.DriveNotFoundException();
        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;
    }