Voglio sapere a livello di programmazione se la mia applicazione è in esecuzione da un'unità di rete. Qual è il modo più semplice per farlo? Dovrebbe supportare entrambi i percorsi UNC (\\127.0.0.1\d$
) e le unità di rete mappate (Z :).Il modo più semplice in C# per scoprire se un'applicazione è in esecuzione da un'unità di rete?
risposta
Questo è il mio metodo attuale di fare questo, ma ci si sente come ci dovrebbe essere un modo migliore.
private bool IsRunningFromNetworkDrive()
{
var dir = AppDomain.CurrentDomain.BaseDirectory;
var driveLetter = dir.First();
if (!Char.IsLetter(driveLetter))
return true;
if (new DriveInfo(driveLetter.ToString()).DriveType == DriveType.Network)
return true;
return false;
}
Questo è per caso di unità mappata. È possibile utilizzare la classe DriveInfo
per scoprire se l'unità a è un'unità di rete o meno.
DriveInfo info = new DriveInfo("Z");
if (info.DriveType == DriveType.Network)
{
// Running from network
}
Metodo completo e codice di esempio.
public static bool IsRunningFromNetwork(string rootPath)
{
try
{
System.IO.DriveInfo info = new DriveInfo(rootPath);
if (info.DriveType == DriveType.Network)
{
return true;
}
return false;
}
catch
{
try
{
Uri uri = new Uri(rootPath);
return uri.IsUnc;
}
catch
{
return false;
}
}
}
static void Main(string[] args)
{
Console.WriteLine(IsRunningFromNetwork(System.IO.Path.GetPathRoot(AppDomain.CurrentDomain.BaseDirectory))); }
In caso utilizzando percorso UNC si è quitely semplice - esaminare il nome host in UNC e di prova che si tratta di localhost (127.0.0.1, :: 1, hostname, hostname.domain.local, indirizzi IP di workstation) o no.
Se il percorso non è UNC - estrarre la lettera di unità dal percorso e testare la classe DriveInfo per il suo tipo
if (new DriveInfo(Application.StartupPath).DriveType == DriveType.Network)
{
// here
}
DriveInfo m = DriveInfo.GetDrives().Where(p => p.DriveType == DriveType.Network).FirstOrDefault();
if (m != null)
{
//do stuff
}
else
{
//do stuff
}
ho riordinato la soluzione di dotnetstep, che è a mio parere migliore perché evita eccezioni quando viene passato un percorso valido, e viene generata un'eccezione se c'è una strada sbagliata passato, che non consente di fare un'ipotesi di vero o falso.
//----------------------------------------------------------------------------------------------------
/// <summary>Gets a boolean indicating whether the specified path is a local path or a network path.</summary>
/// <param name="path">Path to check</param>
/// <returns>Returns a boolean indicating whether the specified path is a local path or a network path.</returns>
public static Boolean IsNetworkPath(String path) {
Uri uri = new Uri(path);
if (uri.IsUnc) {
return true;
}
DriveInfo info = new DriveInfo(path);
if (info.DriveType == DriveType.Network) {
return true;
}
return false;
}
prova:
//----------------------------------------------------------------------------------------------------
/// <summary>A test for IsNetworkPath</summary>
[TestMethod()]
public void IsNetworkPathTest() {
String s1 = @"\\Test"; // unc
String s2 = @"C:\Program Files"; // local
String s3 = @"S:\"; // mapped
String s4 = "ljöasdf"; // invalid
Assert.IsTrue(RPath.IsNetworkPath(s1));
Assert.IsFalse(RPath.IsNetworkPath(s2));
Assert.IsTrue(RPath.IsNetworkPath(s3));
try {
RPath.IsNetworkPath(s4);
Assert.Fail();
}
catch {}
}