开发者

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

开发者 https://www.devze.com 2022-12-23 16:14 出处:网络
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

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 o开发者_运维知识库bject, 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.


                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
                }


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消