2008-12-09 13 views
30

olup olmadığını belirlemek için?Yöntem yolu dize dosya yolu dize yerel makine veya bir uzak sunucuda olup olmadığını belirlemek için, C# veya diğer .NET dili kullanılarak, en iyi yolu nedir yerel veya uzak makine

Bir yol dizesi kullanarak UNC olup olmadığını belirlemek mümkündür aşağıdadır:

: gibi yollar hakkında \ veya diğer sürücü harfi, ama ne: C ile başlayan yollar için inşaat büyük

new Uri(path).IsUnc 

bu UNC yolları vardır ama yine de yereldir - hem yerel makineye bakın

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

cevap

15

Bilmiyorum, ama benim için iş gibi görünüyor:

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

eşleştirilmiş sürücülerle çalışmaz. –

+1

Ayrıca) (Tam bir çözüm – midspace

+0

(. Ya da kendi bilgisayar bağlı değil) yukarı aradığınız uri kapalı veya değil bağlı olduğu ya eğer çalışan yeni System.IO.DriveNotFoundException atmak değil ; – om471987

0

Bunu kontrol etmek için tek bir yöntem bilmiyorum. Ancak, Uri'nin Host özelliğini yerel ana bilgisayar adı veya IP adresiyle karşılaştırabilirsiniz.

kullanarak yerel makine adını alabilir:

string hostName = System.Net.Dns.GetHostName() 
Daha sonra bu dizeyi geçirerek IP adresleri bir dizi alabilirsiniz

:

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

Anahtarı Uri HostNameType mülkiyet, muhtemelen UriHostNameType.Dns veya UriHostNameType.IPv4, isim veya IP adresini eşleşecek. Bunu yapmanın daha verimli bir yol varsa

21

Bu nasıl yaptığımı olduğunu.

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

kullanım değildir – midspace

12

konak var olup olmadığını ekstra çek ile Eric'in cevap .NET 3.5 sürümü:

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

Belki

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

eşlenen sürücüler için ve UNC yolları için çalışmalıdır aşağıdaki. İşte

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

Ben benzer bir ihtiyacı ele nasıl.

 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; 
    } 
İlgili konular