Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#,如何知道文件夹是否位于网络上_C#_Network Programming_Getfiles_Getfileversion - Fatal编程技术网

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

使用C#,如何知道文件夹是否位于网络上,c#,network-programming,getfiles,getfileversion,C#,Network Programming,Getfiles,Getfileversion,使用C#,我希望我的应用程序返回文件夹(具有已知路径)是位于网络中还是位于我的计算机中 如何操作?如果您谈论的是映射网络驱动器,您可以使用DriveInfo: 从另一个这样的问题 使用(pinvoke): 请尝试以下操作: 需要考虑的事项: 以两个反斜杠字符(\)开头的路径被解释为通用命名约定(UNC)路径 以字母开头,后跟冒号(:)的路径被解释为已装入的网络驱动器。但是,PathIsNetworkPath无法识别通过Microsoft MS-DOS SUBST命令或DefinedDevice

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


如何操作?

如果您谈论的是映射网络驱动器,您可以使用
DriveInfo

从另一个这样的问题

使用(pinvoke):

请尝试以下操作:

需要考虑的事项:

  • 以两个反斜杠字符(\)开头的路径被解释为通用命名约定(UNC)路径
  • 以字母开头,后跟冒号(:)的路径被解释为已装入的网络驱动器。但是,PathIsNetworkPath无法识别通过Microsoft MS-DOS SUBST命令或DefinedDevice函数映射到驱动器号的网络驱动器

您可以使用以下方法获取文件夹的UNC路径。不完全是你要找的,但可能有用

    public static string GetUniversalPath(string folderPath)
    {
        if (String.IsNullOrEmpty(folderPath) || folderPath.IndexOf(":") > 1)
            return folderPath;

        if (folderPath.StartsWith("\\"))
        {
            return folderPath;
        }
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" + folderPath.Substring(0, 2) + "'");
        foreach (ManagementObject managementObject in searcher.Get())
        {
            string remoteName = managementObject["RemoteName"] as String;
            if (!String.IsNullOrEmpty(remoteName))
            {
                remoteName += folderPath.Substring(2);
                return remoteName;
            }            
        }
        return folderPath;
    }

基于@jgauffin和@Daniel answers,您可以尝试以下小技巧:

private static bool IsNetwork(String path)
{
    if (path.StartsWith(@"\\"))
       return true;
    var dir = new DirectoryInfo(path);
    var drive = new DriveInfo(dir.Root.ToString());
    return drive.DriveType == DriveType.Network;
}

您是在询问UNC路径吗?关于
\\localhost\something
或映射的网络驱动器如何?对于这两种情况,UNC路径和映射的网络驱动器可能都是重复的。适用于本地路径和映射的网络路径,但不适用于UNC路径。这是覆盖UNC和映射的网络路径的唯一解决方案。对我来说足够好了。
var dirInfo = new DirectoryInfo(yourPath);
var driveInfo = new DriveInfo(dirInfo.Root);
if (driveInfo.DriveType == DriveType.Network)
    Console.WriteLine("Is a network drive!");
class Class
{
    [DllImport("shlwapi.dll")]
    private static extern bool PathIsNetworkPath(string Path);

    [STAThread]
    static void Main(string[] args)
    {
        string strPath = "D:\\Temp\\tmpfile.txt";
        bool blnIsLocalPath = IsLocalPath(strPath);
        Console.WriteLine(blnIsLocalPath.ToString());
        Console.ReadLine();
    }

    private static bool IsLocalPath(string Path)
    {
        return !PathIsNetworkPath(Path);
    }
 }
    public static string GetUniversalPath(string folderPath)
    {
        if (String.IsNullOrEmpty(folderPath) || folderPath.IndexOf(":") > 1)
            return folderPath;

        if (folderPath.StartsWith("\\"))
        {
            return folderPath;
        }
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" + folderPath.Substring(0, 2) + "'");
        foreach (ManagementObject managementObject in searcher.Get())
        {
            string remoteName = managementObject["RemoteName"] as String;
            if (!String.IsNullOrEmpty(remoteName))
            {
                remoteName += folderPath.Substring(2);
                return remoteName;
            }            
        }
        return folderPath;
    }
private static bool IsNetwork(String path)
{
    if (path.StartsWith(@"\\"))
       return true;
    var dir = new DirectoryInfo(path);
    var drive = new DriveInfo(dir.Root.ToString());
    return drive.DriveType == DriveType.Network;
}