Using C#, how to know whether a folder is located on a network or not
Asked Answered
S

6

7

Using C#, I would like my application to return whether a folder (with an already known path) is located in a network or in my computer.

How can I do that?

Shirley answered 10/8, 2011 at 13:52 Comment(3)
Are you asking about UNC paths? What about \\localhost\something, or a mapped network drive?Jordanna
possible duplicate of Check if path is on networkChilung
For the both cases, UNC paths and mapped network drive.Shirley
A
7

If you are talking about a mapped network drive, you can use the DriveInfo's DriveType property:

var driveInfo = new DriveInfo("S:\");
if(driveInfo.DriveType == DriveType.Network)
    // do something
Approximal answered 10/8, 2011 at 13:58 Comment(0)
C
6

Original Answer from another SO question, Check if path is on network.

Use PathIsNetworkPath (pinvoke reference):

class Program
{
    [DllImport("shlwapi.dll")]
    private static extern bool PathIsNetworkPath(string pszPath);

    static void Main(string[] args)
    {
        Console.WriteLine(PathIsNetworkPath("i:\Backup"));
    }
}
Chilung answered 10/8, 2011 at 13:58 Comment(0)
N
3

Based on @jgauffin and @Daniel answers, you could try this little hack:

private static bool IsNetwork(String path)
{
    if (path.StartsWith(@"\\"))
       return true;
    var dir = new DirectoryInfo(path);
    var drive = new DriveInfo(dir.Root.ToString());
    return drive.DriveType == DriveType.Network;
}
Niagara answered 10/8, 2011 at 14:7 Comment(1)
This is the only solution that covers UNC and mapped network paths. Good enough for me.Lombardy
O
0
var dirInfo = new DirectoryInfo(yourPath);
var driveInfo = new DriveInfo(dirInfo.Root);
if (driveInfo.DriveType == DriveType.Network)
    Console.WriteLine("Is a network drive!");
Ovenbird answered 10/8, 2011 at 13:59 Comment(1)
Works for local paths and mapped network paths but not UNC pathsNiagara
W
0

Try the following from Shell Lightweight Utility API:

class Class
{
    [DllImport("shlwapi.dll")]
    private static extern bool PathIsNetworkPath(string Path);

    [STAThread]
    static void Main(string[] args)
    {
        string strPath = "D:\\Temp\\tmpfile.txt";
        bool blnIsLocalPath = IsLocalPath(strPath);
        Console.WriteLine(blnIsLocalPath.ToString());
        Console.ReadLine();
    }

    private static bool IsLocalPath(string Path)
    {
        return !PathIsNetworkPath(Path);
    }
 }

Things to take into consideration:

  • Paths that begin with two backslash characters (\) are interpreted as Universal Naming Convention (UNC) paths.
  • Paths that begin with a letter followed by a colon (:) are interpreted as a mounted network drive. However, PathIsNetworkPath cannot recognize a network drive mapped to a drive letter through the Microsoft MS-DOS SUBST command or the DefineDosDevice function
Whet answered 10/8, 2011 at 14:2 Comment(0)
L
0

You can use folowing method to get UNC path for a folder. Not exactly what you are looking for but might be useful

    public static string GetUniversalPath(string folderPath)
    {
        if (String.IsNullOrEmpty(folderPath) || folderPath.IndexOf(":") > 1)
            return folderPath;

        if (folderPath.StartsWith("\\"))
        {
            return folderPath;
        }
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" + folderPath.Substring(0, 2) + "'");
        foreach (ManagementObject managementObject in searcher.Get())
        {
            string remoteName = managementObject["RemoteName"] as String;
            if (!String.IsNullOrEmpty(remoteName))
            {
                remoteName += folderPath.Substring(2);
                return remoteName;
            }            
        }
        return folderPath;
    }
Leaden answered 10/8, 2011 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.