Deleting A Line From A File

J

Joe Delphi

Hi

Newbie to VB.Net and I have a question

I need to open a text file, read each line, and if I find something in
the line, delete that line from the text file. Can anyone tell me how to
do this. Some of my code is below:

Try
FileOpen(254, "FormSettings.ini", OpenMode.Input)
Do Until EOF(254)
LineOfText = LineInput(254)
FormName = LineOfText.Substring(0, LineOfText.IndexOf("_") -
1)
If FormName = Me.Name Then
(Delete the line from the file somehow......)
Exit Do
Else
FormNameExists = False
End If
Loop
Catch ex As Exception
MsgBox("Error reading FormSettings.ini file")
Finally
FileClose(254)
End Try

Also, is there any method in VB.NET for handling .ini files?

Any help appreciated

JD
 
K

Ken Tucker [MVP]

Hi,

You have to read in each line from the file. Recreate the file with
only the lines you want in the file back to it.

ini file class
http://www.mentalis.org/soft/class.qpx?id=6

Ken
------------------
Hi

Newbie to VB.Net and I have a question

I need to open a text file, read each line, and if I find something in
the line, delete that line from the text file. Can anyone tell me how to
do this. Some of my code is below:

Try
FileOpen(254, "FormSettings.ini", OpenMode.Input)
Do Until EOF(254)
LineOfText = LineInput(254)
FormName = LineOfText.Substring(0, LineOfText.IndexOf("_") -
1)
If FormName = Me.Name Then
(Delete the line from the file somehow......)
Exit Do
Else
FormNameExists = False
End If
Loop
Catch ex As Exception
MsgBox("Error reading FormSettings.ini file")
Finally
FileClose(254)
End Try

Also, is there any method in VB.NET for handling .ini files?

Any help appreciated

JD
 
M

Mythran

Joe Delphi said:
Hi

Newbie to VB.Net and I have a question

I need to open a text file, read each line, and if I find something in
the line, delete that line from the text file. Can anyone tell me how to
do this. Some of my code is below:

Try
FileOpen(254, "FormSettings.ini", OpenMode.Input)
Do Until EOF(254)
LineOfText = LineInput(254)
FormName = LineOfText.Substring(0,
LineOfText.IndexOf("_") -
1)
If FormName = Me.Name Then
(Delete the line from the file somehow......)
Exit Do
Else
FormNameExists = False
End If
Loop
Catch ex As Exception
MsgBox("Error reading FormSettings.ini file")
Finally
FileClose(254)
End Try

Also, is there any method in VB.NET for handling .ini files?

Any help appreciated

JD

First off, you should use an arbitrary number for specifying the file
number. Instead, you should call FreeFile and store the number generated
from that into a variable and use that var instead. Second off, you should
use the System.IO namespace for accessing files :) and forget about using
file numbers completely. That said, let's generate something similar using
the System.IO namespace:

<snip>
Dim sr As StreamReader = IO.File.OpenText("FormSettings.ini")
Dim contents As String

Try
contents = sr.ReadToEnd()
Catch ex As Exception
MsgBox("Error reading FormSettings.ini file")
Finally
sr.Close()
End Try
</snip>

now you can split the contents into an array of strings, a collection,
ArrayList, whatever. when done modifying whichever method you choose, you
can then write the contents back to the file, overwriting what is already
there using a StreamWriter (IO.File.CreateText("FormSettings.ini"))

HTH!

Mythran
 
C

Cor Ligthert

Joe,

As alternative

Roughly changed in this message from this page so watch typos
http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconReadingTextFromFile.asp

\\\
Imports System
Imports System.IO

Class Test
Public Shared Sub Main()
Try
' Create an instance of StreamReader to read from a file.
Dim sr As New StreamReader("TestFile.txt")
Dim sw As New StreamWriter("NewTestFile.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
if line.indexof("Mysearch") <> - 1 then
sw.WriteLine(Line)
end if
Loop Until line Is Nothing
sr.Close()
sw.Close()
file.delete("TestFile.txt")
file.move("NewTestFile.txt", "TextFile")
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("There was an error: ")
Console.WriteLine(E.Message)
End Try
End Sub
End Class
///

I hope this helps,

Cor
 
H

Herfried K. Wagner [MVP]

Joe Delphi said:
I need to open a text file, read each line, and if I find something in
the line, delete that line from the text file. Can anyone tell me how to
do this.

In addition to the other replies: I suggest to write the resulting data to
the original file to prevent the creation timestamp from changing. You can
use 'FileStream.SetLength' to reduce the file's length.
 
C

Cor Ligthert

In addition to the other replies: I suggest to write the resulting data
to the original file to prevent the creation timestamp from changing. You
can use 'FileStream.SetLength' to reduce the file's length.

--
How with my sample, can be interresting?

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top