SimpleDude
asked on
Check if file exists on network path always returns false
Hello, Ive the following code in which I need to open a file if it is available. The file is at a network path (not mapped to a drive letter).
I cannot map to a network drive since this code runs as a service.
My problem is that even if the file exists, Im getting a FALSE result:
Me.Cursor = System.Windows.Forms.Curso rs.WaitCur sor
Dim di2 As New IO.DirectoryInfo("\\192.16 8.10.11\c$ \inetpub\m ailroot\Ba dmail\")
Dim diar2 As IO.FileInfo() = di.GetFiles("*.bad")
Dim dra2 As IO.FileInfo
BRANDFILE.Text = ""
For Each dra2 In diar2
Dim FILE_NAME As String = di2.FullName & dra2.Name
If File.Exists(FILE_NAME) Then
Dim objReader As New System.IO.StreamReader(FIL E_NAME)
Dim FileContent As String = objReader.ReadToEnd
objReader.Close()
objReader.Dispose()
Dim data As String = FileContent
Dim emailRegex As New Regex("\w+([-+.]\w+)*@\w+( [-.]\w+)*\ .\w+([-.]\ w+)*", RegexOptions.IgnoreCase)
Dim emailMatches As MatchCollection = emailRegex.Matches(data)
Dim sb As New StringBuilder()
For Each emailMatch As Match In emailMatches
ListBox1.Items.Add(emailMa tch.Value)
Next
End If
Next
Me.Cursor = System.Windows.Forms.Curso rs.Arrow
Any suggestions that ARE NOT mapping to a network drive (letter)? Thanks.
I cannot map to a network drive since this code runs as a service.
My problem is that even if the file exists, Im getting a FALSE result:
Me.Cursor = System.Windows.Forms.Curso
Dim di2 As New IO.DirectoryInfo("\\192.16
Dim diar2 As IO.FileInfo() = di.GetFiles("*.bad")
Dim dra2 As IO.FileInfo
BRANDFILE.Text = ""
For Each dra2 In diar2
Dim FILE_NAME As String = di2.FullName & dra2.Name
If File.Exists(FILE_NAME) Then
Dim objReader As New System.IO.StreamReader(FIL
Dim FileContent As String = objReader.ReadToEnd
objReader.Close()
objReader.Dispose()
Dim data As String = FileContent
Dim emailRegex As New Regex("\w+([-+.]\w+)*@\w+(
Dim emailMatches As MatchCollection = emailRegex.Matches(data)
Dim sb As New StringBuilder()
For Each emailMatch As Match In emailMatches
ListBox1.Items.Add(emailMa
Next
End If
Next
Me.Cursor = System.Windows.Forms.Curso
Any suggestions that ARE NOT mapping to a network drive (letter)? Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The reason you are getting false is access permission
Your code doesn't run on the same context as you are when you view the files using explorer.
Your code needs to impersonate a user to use when accessing the shared drive.
Yu can install Install-Package SimpleImpersonation to solve this
source ode for the package can be found here https://github.com/mj1856/SimpleImpersonation
Your code doesn't run on the same context as you are when you view the files using explorer.
Your code needs to impersonate a user to use when accessing the shared drive.
Yu can install Install-Package SimpleImpersonation to solve this
source ode for the package can be found here https://github.com/mj1856/SimpleImpersonation
ASKER
Hi Sammy
The Access permission is fine. Both servers have the same administrator password (it is a NLB environment) and I am able to browse \\192.168.10.11\c$ just fine, even edit, or delete files.
The Access permission is fine. Both servers have the same administrator password (it is a NLB environment) and I am able to browse \\192.168.10.11\c$ just fine, even edit, or delete files.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ive found the solution myself, but Split the point between you guys.
ASKER
\\192.168.10.11\c$\inetpub
it already has \ at the end... its really weird that I always get a FALSE