Can anyone plz tell me where what is the code for "Delete a line from text file" command?
or a website to look for it?Searched the delete line but i only got VB6 Code.
Code for "Delete a line from text file"
Page 1 of 14 Replies - 53710 Views - Last Post: 01 March 2011 - 01:29 AM
#1 Guest_NIXZ*
Code for "Delete a line from text file"
Posted 02 September 2008 - 04:56 AM
Replies To: Code for "Delete a line from text file"
#2
Re: Code for "Delete a line from text file"
Posted 02 September 2008 - 05:11 AM
#3
Re: Code for "Delete a line from text file"
Posted 02 September 2008 - 05:13 AM
NIXZ, on 2 Sep, 2008 - 12:56 PM, said:
Can anyone plz tell me where what is the code for "Delete a line from text file" command?
or a website to look for it?Searched the delete line but i only got VB6 Code.
or a website to look for it?Searched the delete line but i only got VB6 Code.
The other topic went down hill because some people (no names) hadn't read the post fully.
The problem arose cause you are trying to use VB6 / VBScript code in a VB.Net project, they are total different programming languages. Just the remember that not all the vb6 & vbscript code snippets out there on the web are 100% compatible with VB.Net.
So here a way of achieving that capability.
Note: The first line in the in file is 0 (Zero) and not 1 (One)
Public Sub DeleteLine(ByRef FileAddress As String, ByRef line As Integer)
Dim TheFileLines As New List(Of String)
TheFileLines.AddRange(System.IO.File.ReadAllLines(FileAddress))
' if line is beyond end of list the exit sub
If line >= TheFileLines.Count Then Exit Sub
TheFileLines.RemoveAt(line)
System.IO.File.WriteAllLines(FileAddress, TheFileLines.ToArray)
End Sub
#4
Re: Code for "Delete a line from text file"
Posted 02 September 2008 - 05:52 AM
To get the original snippet you copied from to work.
First you need to add a reference to Microsoft Scripting Runtime
Project>Add Reference->COM Tab->Microsoft Scripting Runtime->OK
Then alter the code slightly.
Personally i prefer my original solution to using this method, it a lot less code to go wrong. It introduces several bad practices.
First you need to add a reference to Microsoft Scripting Runtime
Project>Add Reference->COM Tab->Microsoft Scripting Runtime->OK
Then alter the code slightly.
Public Function DeleteLine(ByVal fName As String, ByVal LineNumber As Long) _
As Boolean
'Purpose: Deletes a Line from a text file
'Parameters: fName = FullPath to File
' LineNumber = LineToDelete
'Returns: True if Successful, false otherwise
'Requires: Reference to Microsoft Scripting Runtime
'Example: DeleteLine("C:\Myfile.txt", 3)
' Deletes third line of Myfile.txt
'______________________________________________________________
Dim oFSO As New Scripting.FileSystemObject
Dim oFSTR As Scripting.TextStream
Dim ret As Long
Dim lCtr As Long
Dim sTemp As String, sLine As String
Dim bLineFound As Boolean
On Error GoTo ErrorHandler
If oFSO.FileExists(fName) Then
oFSTR = oFSO.OpenTextFile(fName)
lCtr = 1
Do While Not oFSTR.AtEndOfStream
sLine = oFSTR.ReadLine
If lCtr <> LineNumber Then
sTemp = sTemp & sLine & vbCrLf
Else
bLineFound = True
End If
lCtr = lCtr + 1
Loop
oFSTR.Close()
oFSTR = oFSO.CreateTextFile(fName, True)
oFSTR.Write(sTemp)
DeleteLine = bLineFound
End If
ErrorHandler:
On Error Resume Next
oFSTR.Close()
oFSTR = Nothing
oFSO = Nothing
End Function
End Class
Personally i prefer my original solution to using this method, it a lot less code to go wrong. It introduces several bad practices.
#5
Re: Code for "Delete a line from text file"
Posted 01 March 2011 - 01:29 AM
Public Shared Sub RemoveAtLine(ByVal filePath As String, Optional ByVal lineRemove As Integer = -1)
If (String.IsNullOrEmpty(filePath)) Then
Return
End If
Dim lines As New List(Of String)(File.ReadAllLines(filePath))
If (lineRemove >= 0 And lineRemove < lines.Count) Then
lines.RemoveAt(lineRemove )
End If
File.WriteAllLines(filePath, lines.ToArray())
End Sub
Page 1 of 1
|
|

New Topic/Question
Reply
MultiQuote









|