使用C#,如何知道文件夹是否位于网络上

Myr*_*yrR 7 c# network-programming getfiles getfileversion

使用C#,我希望我的应用程序返回文件夹(具有已知路径)是位于网络中还是位于我的计算机中.

我怎样才能做到这一点?

Dan*_*rth 7

如果您正在谈论映射的网络驱动器,您可以使用DriveInfoDriveType属性:

var driveInfo = new DriveInfo("S:\");
if(driveInfo.DriveType == DriveType.Network)
    // do something
Run Code Online (Sandbox Code Playgroud)


Bra*_*tie 5

另一个SO问题的原始答案检查路径是否在网络上

使用PathIsNetworkPath(pinvoke 参考):

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

    static void Main(string[] args)
    {
        Console.WriteLine(PathIsNetworkPath("i:\Backup"));
    }
}
Run Code Online (Sandbox Code Playgroud)