I'm trying to detect if the directory exists, but in this particular situation my directory is a network location. I used VB.NET's My.Computer.FileSystem.DirectoryExists(PATH)
and the more general System.IO.Directory.Exists(PATH)
, and in both cases, the system response is false. I checked and the PATH exists, I can view it in MyComputer Folder. If I debug the program, and watch the My.Computer.FileSystem.Drives
variable, the network location does not appear in that list.
UPDATE: I checked and in Windows XP the Response is True, but not in Windows 7.
UPDATE2: I tested both proposed solutions but I still have the same problem, on the image below you will see that I can access using Explorer but my program cannot. The GetUNCPath
function returns a valid path (no errors), but Directory.Exists
stil returns false.
I also tried with the UNC path "\\Server\Images"; same result.
UPDATE3: If I cannot link with a network drive, how can I link to UNC path directly?. I discovered that if i run VS in normal mode, it works, but my software must run in administrator mode. So, there is any way to check the existence of a network directory as administrator?
If UAC is turned on, mapped network drives only exist "by default" in the session they are mapped: Normal or elevated. If you map a network drive from explorer, then run VS as admin, the drive will not be there.
You need to enable what MS calls "linked connections": HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System: EnableLinkedConnections (REG_DWORD) = 0x1
Background Information about "two logon sessions" with UAC: http://support.microsoft.com/kb/937624/en-us
When you use System.IO.Directory.Exists
, it only lets you know that it couldn't find the directory, but this could be because the directory doesn't actually exist or because the user doesn't have sufficient access rights to the directory.
In order to resolve this, we add a secondary test after Directory.Exists
fails to obtain the real reason for the directory's absence and we have wrapped this into a global method that is used in place of the standard Directory.Exists
method:
''' <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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With