Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a folder is a local or a network resource in .NET

Is there a quick way to check whether a path I have is on a local disk or somewhere on the network? I can't just check to see if it's a drive letter vs. UNC, because that would incorrectly identify mapped drives as local. I assumed it would be a boolean in the DirectoryInfo object, but it appears that it's not.

I've found classic VB code to do this check (through an API), but nothing for .NET so far.

like image 373
SqlRyan Avatar asked Mar 16 '10 14:03

SqlRyan


1 Answers

                System.IO.DirectoryInfo di;
                if (System.IO.Path.IsPathRooted(di.FullName))
                {
                    System.IO.DriveInfo drive = new System.IO.DriveInfo(System.IO.Path.GetPathRoot(di.FullName));
                    if (drive.DriveType == System.IO.DriveType.Network)
                    {
                        // do something
                    }
                }
                else // shouldn't be reached
                {
                    // relative path => local
                }
like image 184
Seb Avatar answered Sep 27 '22 21:09

Seb