检查一个文件夹是否是本地或.NET的网络资源 [英] Check whether a folder is a local or a network resource in .NET

查看:136
本文介绍了检查一个文件夹是否是本地或.NET的网络资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种快速的方法来检查一个路径我是否在本地磁盘或网络上的某个地方?我不能只检查,看它是否是一个驱动器号与UNC,因为那样会错误地标识映射驱动器本地。我认为这将是在DirectoryInfo对象一个布尔值,但现在看来,事实并非如此。

我发现经典的VB code做此项检查(通过API),但没有对.NET至今。

解决方案

  System.IO.DirectoryInfo迪;
                如果(System.IO.Path.IsPathRooted(di.FullName))
                {
                    System.IO.DriveInfo驱动器=新System.IO.DriveInfo(System.IO.Path.GetPathRoot(di.FullName));
                    如果(drive.DriveType == System.IO.DriveType.Network)
                    {
                        // 做一点事
                    }
                }
                否则//不应该达到
                {
                    //相对路径=>本地
                }
 

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.

解决方案

                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
                }

这篇关于检查一个文件夹是否是本地或.NET的网络资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆