troubleshooting Question

VB.Net If File.Exists Doesn't Return False When File Doesn't Exist

Avatar of jschlemmer
jschlemmer asked on
.NET ProgrammingVB ScriptVisual Basic.NET
10 Comments1 Solution1902 ViewsLast Modified:
I am building an application that is supposed to find a file and process it but needs to generate message box alerting user if the file specified doesn't exist.  When I run the program and the file does exist, it works flawlessly.  However, when I run the program and the file doesn't exist, it literally doesn't do anything.  No message box, no errors, absolutely nothing.  It behaves as if it ran through the program successfully and performs the me.close at the end of the script.  Below is the code:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each q As String In Directory.GetFiles("C:\","InSight_Customized_View_Download*.csv")
            Dim FILE_NAME As String = q
            Dim filename As String = "C:\Error Log\Error Log.txt"
            Dim TextLine As String

            If File.Exists(FILE_NAME) = True Then

               'A bunch of code that executes if file exists.  This runs without any issue.

            Else

                Dim sb As New StringBuilder()
                Using objReader2 As New StreamReader(filename)
                    sb.AppendLine(objReader2.ReadToEnd)
                    sb.AppendLine(Today & vbTab & vbTab & "File Does Not Exist")
                    objReader2.Close()
                End Using

                Dim objWriter As New StreamWriter(filename)
                objWriter.Write(sb.ToString)
                objWriter.Close()

                MsgBox("File Does Not Exist")

            End If
        Next
        Me.Close()
    End Sub


Even though file does not exist, it does not write to error log nor does it generate message box.  It just performs the me.close.
ASKER CERTIFIED SOLUTION
jschlemmer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros