Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

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.

Thanks.
0
dynamicrevolutions
Asked:
dynamicrevolutions
1 Solution
 
twopienkaCommented:
You could use this command: System.IO.File.Exists("C:\test.txt")

HTH

Thomas
0
 
planoczCommented:
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

        Try
            ' Check to see if the user is writing over an existing file.
            If File.Exists(sFileName) = True Then
                sType = File.AppendText(sFileName)
                myStreamWriter = sType
            Else
                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.
            myStreamWriter.Write(sData)
            myStreamWriter.Flush()
        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)
        Finally
            ' Close the object if it has been created.
            If Not myStreamWriter Is Nothing Then
                myStreamWriter.Close()
            End If
        End Try
    End Sub
0
 
willcode4coffeeCommented:
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.

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

Thanks for others too.

Cheers!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now