By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,061 Members | 1,539 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,061 IT Pros & Developers. It's quick & easy.

Deleting A Line From A File

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
------------------
"Joe Delphi" <de*******@nospam.earthlink.net> wrote in message
news:5j****************@newsread2.news.pas.earthli nk.net...
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


Nov 21 '05 #2

P: n/a

"Joe Delphi" <de*******@nospam.earthlink.net> wrote in message
news:5j****************@newsread2.news.pas.earthli nk.net...
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

Nov 21 '05 #3

P: n/a
Joe,

As alternative

Roughly changed in this message from this page so watch typos
http://msdn.microsoft.com/library/de...xtFromFile.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
Nov 21 '05 #4

P: n/a
"Joe Delphi" <de*******@nospam.earthlink.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

P: n/a
>
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
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.