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

2019-02-08 01:27发布

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

6条回答
Lonely孤独者°
2楼-- · 2019-02-08 01:42
DriveInfo m = DriveInfo.GetDrives().Where(p => p.DriveType == DriveType.Network).FirstOrDefault();
if (m != null)
{
    //do stuff
}
else
{
    //do stuff
}
查看更多
手持菜刀,她持情操
3楼-- · 2019-02-08 01:46

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

查看更多
何必那么认真
4楼-- · 2019-02-08 01:51

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;
    }
查看更多
来,给爷笑一个
5楼-- · 2019-02-08 01:51

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 {}
}
查看更多
欢心
6楼-- · 2019-02-08 01:57

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)));    }
查看更多
虎瘦雄心在
7楼-- · 2019-02-08 01:57
if (new DriveInfo(Application.StartupPath).DriveType == DriveType.Network)
{    
    // here   
}
查看更多
登录 后发表回答