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

6

19

I want to programmatically find out if my application is running from a network drive. What is the simplest way of doing that? It should support both UNC paths (\\127.0.0.1\d$) and mapped network drives (Z:).

Unquote answered 26/12, 2011 at 7:6 Comment(0)
C
23

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)));    }
Coulee answered 26/12, 2011 at 7:11 Comment(0)
L
4
if (new DriveInfo(Application.StartupPath).DriveType == DriveType.Network)
{    
    // here   
}
Lamar answered 26/12, 2011 at 7:17 Comment(0)
U
2

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;
    }
Unquote answered 26/12, 2011 at 7:17 Comment(0)
T
2

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 {}
}
Timepleaser answered 21/10, 2016 at 6:50 Comment(0)
I
1

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

Inclusion answered 26/12, 2011 at 7:16 Comment(0)
S
0
DriveInfo m = DriveInfo.GetDrives().Where(p => p.DriveType == DriveType.Network).FirstOrDefault();
if (m != null)
{
    //do stuff
}
else
{
    //do stuff
}
Siloum answered 26/12, 2011 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.