2011-12-26 14 views

Respuesta

2

Este es mi método actual de hacer esto, pero se siente como debería haber una mejor manera.

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

Esto es para la unidad de unidad mapeada. Puede usar la clase DriveInfo para averiguar si la unidad a es una unidad de red o no.

DriveInfo info = new DriveInfo("Z"); 
if (info.DriveType == DriveType.Network) 
{ 
    // Running from network 
} 

Método completo y código de muestra.

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

En caso utilizando ruta UNC es Quitely sencilla - examinar nombre de host en la UNC y la prueba de que es localhost (127.0.0.1, :: 1, nombre de host, hostname.domain.local, direcciones IP de estación de trabajo) o no.

Si la ruta no es UNC - extraer la letra de unidad de la trayectoria y probar la clase DriveInfo para su tipo

4
if (new DriveInfo(Application.StartupPath).DriveType == DriveType.Network) 
{  
    // here 
} 
0
DriveInfo m = DriveInfo.GetDrives().Where(p => p.DriveType == DriveType.Network).FirstOrDefault(); 
if (m != null) 
{ 
    //do stuff 
} 
else 
{ 
    //do stuff 
} 
0

reacomodé la solución de dotnetstep, que es en mi opinión es mejor porque evita excepciones cuando se pasa una ruta válida, y se produce una excepción si hay un camino equivocado pasado, que no permite hacer una suposición de verdadero 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; 
} 

prueba:

//---------------------------------------------------------------------------------------------------- 
/// <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 {} 
} 
Cuestiones relacionadas