Easiest way in C# to find out if an app is running

2019-02-08 01:25发布

问题:

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:).

回答1:

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


回答2:

if (new DriveInfo(Application.StartupPath).DriveType == DriveType.Network)
{    
    // here   
}


回答3:

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


回答4:

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



回答5:

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


回答6:

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