How do I confirm that a file is really there before I look at it with FileOpen.

I am using the FileOpen object to pull text from a file.  I want to make sure that before I hand it over to the object that the file is really there.  I want to be able to kick up a error message for the end user that will not take them out of the program.

here is the code that I am using.

            FileOpen(1, Me.txtEmailLocation.Text, OpenMode.Input)
            While Not EOF(1)
                txtLine = txtLine & vbCrLf & LineInput(1)
            End While
            FileClose(1)

Thanks.
Richard ComitoDirector of ITAsked:
Who is Participating?
 
NetworkArchitekCommented:
Hi GabicusC,
Well, this should work

Imports System.IO

If File.Exists("c:\somefile.txt") Then
'code
Else
'code  
End If

Hope this helps.

Cheers!
0
 
Richard ComitoDirector of ITAuthor Commented:
I hope this is not too much of a newbie question but when I input:
imports System.IO  I get a "syntax Error"

and then on the file I receive a "Name file is not declared".

This is the way I entered in the code.

        If Me.lstEmailType.SelectedItem.ToString() = "LA Stage Tix" Then
        Else
            imports System.IO
            If File.Exists(Me.lstEmailType.SelectedItem.ToString() ) Then
                FileOpen(1, Me.txtEmailLocation.Text, OpenMode.Input)
                While Not EOF(1)
                    txtLine = txtLine & vbCrLf & LineInput(1)
                End While
                FileClose(1)
            Else
                MsgBox("The file you have entered does not exists.  Please check your path and then try agian.")
                Me.txtEmailLocation.Focus()
                GoTo lastline
            End If

        End If
0
 
NetworkArchitekCommented:
Well, the "imports" statement just allows you to use that namespace. You want to invoke that command before any class declarations. Other than that your code is fine, but you can't "import" inside an else clause like that. If you have ever used C or any language like that all you are doing is essentially settings your "includes."
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
wguerramCommented:
Enclose the rountine in a Try - Catch
This way the program won't kick the user

Try
      FileOpen(1, Me.txtEmailLocation.Text, OpenMode.Input)
            While Not EOF(1)
                txtLine = txtLine & vbCrLf & LineInput(1)
            End While
catch ex as exception
    msgbox ex.Message
finally
       FileClose(1)
end try
0
 
toddhdCommented:
NetworkArchitek got it right - the imports system.io should be the very first line in your file. Everything else looks good at first glance.
0
 
Richard ComitoDirector of ITAuthor Commented:
Well this is a tough one.  NetworkArchitek gave me the answer, but toddhd just made it a little more clear. How do I work the accepting on this?

Thanks for all your help.
0
 
toddhdCommented:
You can choose to split points if you wish - just choose "assisted answer"
0
 
wguerramCommented:
I consider you should give the points to NetworkArchitek

Since He assummed you knew where to write the statement imports system.io and He Answered your Question.
0
 
toddhdCommented:
I thought I clearly indicated that NetworkArchitek had given the correct answer. Also, I did not "assume" that GabicusC knew where to put the Imports statement - if he did, he would have done so in the first place. I just tried to clarify the proper action to take to minimize his steps to correct the problem. That's why the assisted answer exists. But whatever - like Drew Carey says on "Who's line is it anyway":

"Everything is made up, and the points don't count anyway" :)

So credit it however you like, I don't need the points. I just wanted to help.
0
 
Richard ComitoDirector of ITAuthor Commented:
Just wanted to make sure like that I did the right thing.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.