Easiest way in C# to find out if an app is running from a network drive?

北城以北 提交于 2019-11-30 06:47:09

This is for mapped drive case. You can use the DriveInfo class to find out whether drive a is a network drive or not.

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

Complete method and Sample Code.

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)));    }
Ali Foroughi
if (new DriveInfo(Application.StartupPath).DriveType == DriveType.Network)
{    
    // here   
}

This is my current method of doing this, but it feels like there should be a better way.

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

In case using UNC path it is quitely simple - examine host name in UNC and test that it is localhost(127.0.0.1, ::1, hostname, hostname.domain.local, ip-addresses of workstation) or not.

If the path is not UNC - extract the drive letter from path and test the DriveInfo class for its type

Mustafa Ekici
DriveInfo m = DriveInfo.GetDrives().Where(p => p.DriveType == DriveType.Network).FirstOrDefault();
if (m != null)
{
    //do stuff
}
else
{
    //do stuff
}

I rearranged the solution of dotnetstep, which is in my opinion better because it avoids exceptions when a valid path is passed, and it throws an exception if there is a wrong path passed, which does not allow to make an assumption of true or false.

//----------------------------------------------------------------------------------------------------
/// <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 {}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!