[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.net File Operation

Posted on 2006-05-31
2
Medium Priority
?
231 Views
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
                FileClose(1)
            Next
        End If


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

Accepted Solution

by:
Bob Learned earned 1200 total points
ID: 16802993
1) .NET version?

2) For 2003, try this:

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

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

Bob
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 800 total points
ID: 16803064
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".

Roger
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

873 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