Solved File Operation

Posted on 2006-05-31
Last Modified: 2010-08-05
I am trying to open a exisiting text file in append mode , and add two lines,one in the beginning and one in the end.

If (System.IO.Directory.GetFiles(Path).Length > 0) Then
            Dim sFile As String
            For Each sFile In System.IO.Directory.GetFiles(Path)
                Dim filename = System.IO.Path.GetFileName(sFile)
                FileOpen(1, Path + filename, OpenMode.Append, OpenAccess.ReadWrite)
                While EOF(1)
                    strTempRead = LineInput(1)   /// ERROR
                End While
        End If

./// ERROR  
An unhandled exception of type 'System.IO.IOException' occurred in microsoft.visualbasic.dll
- Additional information: Bad file mode
Question by:bender007
    LVL 96

    Accepted Solution

    1) .NET version?

    2) For 2003, try this:

        Dim reader As New IO.StreamReader(fileName)
        Dim fileText As String = reader.ReadToEnd()

    3) For 2005, try this:
       Dim fileText As String = My.Computer.FileSystem.ReadAllText(fileName)

    LVL 34

    Assisted Solution

    Or, if you want to stick with FileOpen, I think you'll have to use OpenMode.Input rather than OpenMode.Append if you want to use LineInput.

    But the code you show isn't actually doing anything useful - just continually writing lines to strTempRead, each new write overwriting what was there before.  It certainly doesn't "add two lines,one in the beginning and one in the end".


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now