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

Posted on 2004-11-17
Last Modified: 2010-04-23
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.

Question by:dynamicrevolutions
    LVL 1

    Accepted Solution

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


    LVL 27

    Expert Comment

    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
    LVL 4

    Expert Comment

    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.

    LVL 5

    Author Comment

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

    Thanks for others too.


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now