Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 361
Doron Yaacoby Avatar asked Dec 26 '11 07:12

Doron Yaacoby


People also ask

Is C easy to learn for beginners?

While C is one of the more difficult languages to learn, it's still an excellent first language pick up because almost all programming languages are implemented in it. This means that once you learn C, it'll be simple to learn more languages like C++ and C#.

Can I learn C in a week?

The Basic Syntax: A Few Days – 1 Week I think it's safe to say you can learn this within the first few days to a week of picking up the language. The syntax for C is actually pretty simplistic. It's the easier part of picking up the language.

Can I learn C in 2 weeks?

You can master C language in 2 weeks. But to master C programming it will take years. Since your question is about C language, I have explained below how to master C language in 2 weeks.


1 Answers

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)));    }
like image 157
dotnetstep Avatar answered Oct 01 '22 11:10

dotnetstep