views:

88

answers:

3

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.

+1  A: 

You could start with the UNC-check. Then, if it is not a UNC path, create a DriveInfo object for the drive and check the DriveType.

Fredrik Mörk
A: 

From the drive letter in the path, get a DriveInfo instance. This has a DriveType property, which can be: CDRom, Fixed, Unknown, Network, NoRootDirectory, Ram, Removable, or Unknown

Timores
+1  A: 
                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
                }
Seb