2011-12-26 60 views

Trả lời

2

Đây là phương pháp hiện tại của tôi để làm điều này, nhưng nó cảm thấy như có phải là một cách tốt hơn.

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

Đây là trường hợp ổ đĩa được ánh xạ. Bạn có thể sử dụng lớp DriveInfo để tìm hiểu xem ổ đĩa a có phải là ổ đĩa mạng hay không.

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

Phương pháp hoàn chỉnh và Mã mẫu.

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

Trong trường hợp sử dụng đường dẫn UNC nó là quitely đơn giản - kiểm tra tên máy chủ trong UNC và kiểm tra rằng nó là localhost (127.0.0.1, :: 1, hostname, hostname.domain.local, địa chỉ ip của máy trạm) hay không.

Nếu đường dẫn không UNC - trích xuất các ký tự ổ đĩa từ đường dẫn và kiểm tra các lớp DriveInfo cho loại hình của nó

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

Tôi sắp xếp lại các giải pháp của dotnetstep, đó là theo ý kiến ​​của tôi tốt hơn bởi vì nó tránh trường hợp ngoại lệ khi một đường dẫn hợp lệ được thông qua, và nó ném một ngoại lệ nếu có một con đường sai trôi qua, không cho phép giả định đúng hay sai.

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

Test:

//---------------------------------------------------------------------------------------------------- 
/// <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 {} 
} 
Các vấn đề liên quan