By using FileOpen method, how do I check whether the file is existed or not?

When I use FileOpen method in VB.NET, the open mood have few types, my program need to decide if the file is existed, open mood would be as Append; otherwise, open mood would be as Output.

From this case, how do I check whether the file I am trying to open exists or not?

It's urgent matter, so I award this question to 500 points.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You could use this command: System.IO.File.Exists("C:\test.txt")



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sample of making a file or append to ....
The sample has a Header line and then add the data

 ' This subrouting uses a StreamWriter object to create a new file
    '   and fill it with the text in sFileName. It first checks to see
    '   if the file already exists of so then it Appends else overwrites it.
    Public Sub StreamWriterCREATEFile(ByVal sFileName As String, ByVal sData As String)
        Dim sType As System.IO.StreamWriter
        Dim myStreamWriter As StreamWriter
        Dim sHeader As String

            ' Check to see if the user is writing over an existing file.
            If File.Exists(sFileName) = True Then
                sType = File.AppendText(sFileName)
                myStreamWriter = sType
                sType = File.CreateText(sFileName)
                myStreamWriter = sType
                sHeader = "ID, Name" & Space(20) & ", Address" & Space(20) & ", Address2" & Space(20) & _
                          ",City" & Space(15) & ",ST,Zip" & Space(10) & ",Contact" & Space(20) & ",Phone" & _
                           Space(15) & ",ShipWht,Tracking#" & Space(10) & ",Carrier Type" & Space(10) & ",Cost" & _
                           Space(10) & ",Print Date,MailCarrierID,Cancelled" & Space(10) & "Send To Error" & vbCrLf
                sData = (sHeader & sData)
            End If
            ' Write the entire contents of the sData string to the StreamWriter in one shot.
        Catch exc As Exception
            MsgBox("File could not be created or written to." + vbCrLf + _
                "Please verify that the filename is correct, " + _
                "and that you have write permissions for the desired " + _
                "directory." + vbCrLf + vbCrLf + "Exception: " + exc.Message)
            ' Close the object if it has been created.
            If Not myStreamWriter Is Nothing Then
            End If
        End Try
    End Sub
I don't think you need any code. If I am not mistaken, Append opens it for append if it exists or creates it and opens it for append if it does not exist.

dynamicrevolutionsAuthor Commented:
Well, I think Thomas gave me a straight answer that I need. :-) thanks thomas.

Thanks for others too.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.