We help IT Professionals succeed at work.
Get Started

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

1,883 Views
Last Modified: 2013-11-26
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.
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE