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

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

更新:我检查,并在Windows XP的响应是真实的,但不是在Windows 7。

更新2:我testing了两个build议的解决scheme,但我仍然有同样的问题,在下面的图像中,你会看到,我可以访问使用资源pipe理器,但我的程序不能。 GetUNCPath函数返回一个有效的path(没有错误),但Directory.Exists stil返回false。

我也尝试过使用UNCpath“\\ Server \ Images”; 同样的结果。

在这里输入图像说明

UPDATE3:如果我无法链接到networking驱动器,我怎样才能直接链接到UNCpath? 我发现,如果我在正常模式下运行VS,它的工作原理,但我的软件必须以pipe理员模式运行。 那么,有没有办法以pipe理员身份检查networking目录的存在?

如果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

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

为了解决这个问题,在Directory.Exists未能获得目录缺失的真正原因之后,我们添加了一个辅助测试,我们已经把它包装到一个全局方法中,用来代替标准的Directory.Exists方法:

 ''' <summary> ''' This method tests to ensure that a directory actually does exist. If it does not, the reason for its ''' absence will attempt to be determined and returned. The standard Directory.Exists does not raise ''' any exceptions, which makes it impossible to determine why the request fails. ''' </summary> ''' <param name="sDirectory"></param> ''' <param name="sError"></param> ''' <param name="fActuallyDoesntExist">This is set to true when an error is not encountered while trying to verify the directory's existence. This means that ''' we have access to the location the directory is supposed to be, but it simply doesn't exist. If this is false and the directory doesn't exist, then ''' this means that an error, such as a security error, was encountered while trying to verify the directory's existence.</param> Public Function DirectoryExists(ByVal sDirectory As String, ByRef sError As String, Optional ByRef fActuallyDoesntExist As Boolean = False) As Boolean ' Exceptions are partially handled by the caller If Not IO.Directory.Exists(sDirectory) Then Try Dim dtCreated As Date ' Attempt to retrieve the creation time for the directory. ' This will usually throw an exception with the complaint (such as user logon failure) dtCreated = Directory.GetCreationTime(sDirectory) ' Indicate that the directory really doesn't exist fActuallyDoesntExist = True ' If an exception does not get thrown, the time that is returned is actually for the parent directory, ' so there is no issue accessing the folder, it just doesn't exist. sError = "The directory does not exist" Catch theException As Exception ' Let the caller know the error that was encountered sError = theException.Message End Try Return False Else Return True End If End Function 
 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 az && AZ. 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),当我看到一个网络驱动器,我看到计算机>图像(\\ xyzserver) 你的PC的名字是Equipo吗? 是西班牙吗? 是你的电脑吗? 我试图重现你的问题,但它适用于我:

在这里输入图像说明

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

张贴的各种解决方案不适合我,所以这里是我自己的:

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