Metodo per determinare se la stringa del percorso è una macchina locale o remota
Domanda
Qual è il modo migliore, usando C # o altro linguaggio .NET, per determinare se una stringa del percorso del file si trova sul computer locale o su un server remoto?
È possibile determinare se una stringa di percorso è UNC utilizzando quanto segue:
new Uri(path).IsUnc
Funziona benissimo per i percorsi che iniziano con C: \ o altra lettera di unità, ma per quanto riguarda i percorsi come:
\\machinename\sharename\directory
\\10.12.34.56\sharename\directory
... dove entrambi si riferiscono al computer locale - questi sono percorsi UNC ma sono ancora locali.
Soluzione
Non so se esiste un modo più efficiente per farlo, ma sembra funzionare per 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;
}
}
}
Altri suggerimenti
Ecco come l'ho fatto.
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();
}
Versione .NET 3.5 della risposta di Eric con un ulteriore controllo sull'esistenza dell'host:
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));
}
Quanto segue dovrebbe funzionare per le unità mappate e per i percorsi 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);
Ecco come ho affrontato un'esigenza simile.
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;
}
Forse
var isLocal = Dns.GetHostName() == _host || Dns.GetHostEntry(Dns.GetHostName()).AddressList.Any(i => i.ToString().Equals(_host));
Non conosco un solo metodo per verificarlo. Tuttavia, puoi confrontare la proprietà Host
di Uri con il nome host locale o l'indirizzo IP.
Puoi ottenere il nome del computer locale usando:
string hostName = System.Net.Dns.GetHostName()
È quindi possibile ottenere una matrice di indirizzi IP passando quella stringa a:
System.Net.IPAddress[] addresses = System.Net.Dns.GetHostAddresses(hostName);
Attiva la proprietà HostNameType
di Uri, probabilmente UriHostNameType.Dns
o UriHostNameType.IPv4
, per abbinare il nome o l'indirizzo IP.