Link to home
Start Free TrialLog in
Avatar of SimpleDude
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.Cursors.WaitCursor
        Dim di2 As New IO.DirectoryInfo("\\192.168.10.11\c$\inetpub\mailroot\Badmail\")
        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(FILE_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(emailMatch.Value)
                Next

            End If
        Next
        Me.Cursor = System.Windows.Forms.Cursors.Arrow


Any suggestions that ARE NOT mapping to a network drive (letter)? Thanks.
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SimpleDude
SimpleDude

ASKER

I tried it... di2 content is:

\\192.168.10.11\c$\inetpub\mailroot\Badmail\

it already has \ at the end... its really weird that I always get a FALSE
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
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ive found the solution myself, but Split the point between you guys.