检查网络驱动器上是否存在目录

时间:2013-07-04 13:55:28

标签: c# windows vb.net .net-4.0

我正在尝试检测目录是否存在,但在这种特殊情况下,我的目录是网络位置。 我使用了VB.NET的My.Computer.FileSystem.DirectoryExists(PATH)和更通用的System.IO.Directory.Exists(PATH),在这两种情况下,系统响应都是错误的。 我检查了PATH,我可以在MyComputer文件夹中查看它。 如果我调试程序并观察My.Computer.FileSystem.Drives变量,则网络位置不会出现在该列表中。

更新:我检查过,在Windows XP中,响应为真,但在Windows 7中没有。

UPDATE2:我测试了两个建议的解决方案,但我仍然遇到同样的问题,在下图中你会看到我可以使用资源管理器进行访问,但我的程序却无法访问。 GetUNCPath函数返回有效路径(无错误),但Directory.Exists stil返回false。

我还尝试使用UNC路径“\\ Server \ Images”;同样的结果。

enter image description here

UPDATE3:如果我无法与网络驱动器链接,我该如何直接链接到UNC路径?我发现如果我在正常模式下运行VS,它可以工作,但我的软件必须以管理员模式运行。那么,有没有办法以管理员的身份检查网络目录是否存在?

4 个答案:

答案 0 :(得分:14)

如果启用了UAC,则映射的网络驱动器仅在其映射的会话中“默认”存在:正常或已提升。如果您从资源管理器映射网络驱动器,然后以管理员身份运行VS,驱动器将不在那里。

您需要启用MS称为“链接连接”的内容: HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ CurrentVersion \ Policies \ System:EnableLinkedConnections(REG_DWORD)= 0x1

有关UAC“两次登录会话”的背景信息:http://support.microsoft.com/kb/937624/en-us

答案 1 :(得分:8)

当您使用System.IO.Directory.Exists时,它只会让您知道找不到该目录,但这可能是因为该目录实际上并不存在,或者因为该用户没有足够的访问权限目录。

为了解决这个问题,我们在Directory.Exists无法获得目录缺席的真正原因之后添加了一个辅助测试,我们将其包装成一个全局方法,用于代替标准{{1方法:

Directory.Exists

答案 2 :(得分:5)

public static class MappedDriveResolver
    {
        [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int WNetGetConnection([MarshalAs(UnmanagedType.LPTStr)] string localName, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, ref int length);
        public static string GetUNCPath(string originalPath)
        {
            StringBuilder sb = new StringBuilder(512);
            int size = sb.Capacity;

            // look for the {LETTER}: combination ...
            if (originalPath.Length > 2 && originalPath[1] == ':')
            {
                // don't use char.IsLetter here - as that can be misleading
                // the only valid drive letters are a-z && A-Z.
                char c = originalPath[0];
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                {
                    int error = WNetGetConnection(originalPath.Substring(0, 2), sb, ref size);
                    if (error == 0)
                    {
                        DirectoryInfo dir = new DirectoryInfo(originalPath);
                        string path = Path.GetFullPath(originalPath).Substring(Path.GetPathRoot(originalPath).Length);
                        return Path.Combine(sb.ToString().TrimEnd(), path);
                    }
                }
            }    
            return originalPath;
        }
    }

要使用它,请传入网络文件夹路径,转换为UNC文件夹路径并查看该文件夹是否存在:

File.Exists(MappedDriveResolver.GetUNCPath(filePath));

修改

当我查看网络驱动器时,我看到了您的第二次编辑和唯一的区别(在我的Windows7中),我看到计算机&gt;图像(\\ xyzServer)。你的电脑名称是Equipo吗?是西班牙语的团队吗?那是你的电脑吗?我试图重现你的问题,但它对我有用:

enter image description here

答案 3 :(得分:1)

除此之外,我还需要对可能列出的网络共享进行“存在”检查,但该帐户没有访问权限,因此Directory.Exists将返回False。

发布的各种解决方案对我不起作用,所以这是我自己的:

public static bool DirectoryVisible(string path)
{
    try
    {
        Directory.GetAccessControl(path);
        return true;
    }
    catch (UnauthorizedAccessException)
    {
        return true;
    }
    catch
    {
        return false;
    }
}