|
-
Jul 9th, 2009, 04:09 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED]Deleting a line in a text file that ends with a certain string
Hey,
I need help deleting a line from a text file. This is for an ASP.net Webpage. I will have a file called Sources.txt. Example data of this file looks like this:
John|[email protected]
Bob|[email protected]
I would like to delete the line ending in |[email protected] programmatically. This will obviously be dynamic. I want to do this by determining what line number it is at, unless of course, there is a better way.
I want to search for the line by the ending of it so people cannot put [email protected]|[email protected] and accidentally get [email protected] deleted.
This is for an unsubscribe link.
Code:
Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
lines.RemoveAt(1)
IO.File.WriteAllLines("Sources.txt", lines.ToArray())
'Stop people from refreshing and deleting other records.
Session.Clear()
End Sub
Thanks
Last edited by samtheman; Jul 9th, 2009 at 06:44 PM.
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jul 9th, 2009, 04:37 PM
#2
Re: Deleting a line in a text file that ends with a certain string
Have you looked at String.EndsWith ?
-
Jul 9th, 2009, 05:55 PM
#3
Thread Starter
Hyperactive Member
Re: Deleting a line in a text file that ends with a certain string
Hi,
This will not work with a list. Instead I shall try using List.Find, letting you know how I get on.
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jul 9th, 2009, 06:01 PM
#4
Thread Starter
Hyperactive Member
Re: Deleting a line in a text file that ends with a certain string
Ok, I have found I can get it to work. But I need help
Code:
Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
Dim DataList As String = lines.ToString
If DataList.EndsWith("|[email protected]") Then
lines.....
End If
I want to go through the entry and anything matching If DataList.EndsWith("|[email protected]"), I want to get the Index and then delete it with
I then want to keep going through it so I scan the whole document.
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jul 9th, 2009, 06:06 PM
#5
Re: Deleting a line in a text file that ends with a certain string
Why don't you go trough each line and if that line doesnt endswith that string, just add the string to a new list?
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Jul 9th, 2009, 06:17 PM
#6
Thread Starter
Hyperactive Member
Re: Deleting a line in a text file that ends with a certain string
Hey,
This seems a little RAM inefficient, but it seems to be the only way, I tried to do an If String.Endswith = .... then GetIndexOf but it failed.
Can you give me some code to suggest this method of yours? Baring in mind after removal I was planning to recreate the file with
vb Code:
IO.File.WriteAllLines("Sources.txt", lines.ToArray())
and that there may be several R/Ws going on at once.
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jul 9th, 2009, 06:25 PM
#7
Re: Deleting a line in a text file that ends with a certain string
Try this:
VB.NET Code:
Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt")) Dim finalLines As New List(Of String) For Each line As String In lines finalLines.Add(line) End If Next
Good luck
Last edited by mickey_pt; Jul 9th, 2009 at 06:25 PM.
Reason: Code...
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Jul 9th, 2009, 06:31 PM
#8
Thread Starter
Hyperactive Member
Re: Deleting a line in a text file that ends with a certain string
Will try after a 10 minute break
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jul 9th, 2009, 06:43 PM
#9
Thread Starter
Hyperactive Member
Re: Deleting a line in a text file that ends with a certain string
Perfect my good man, will give you rep and mark as resolved. I added this: IO.File.WriteAllLines("Sources.txt", finalLines.ToArray()) so that I write the final lines to the TXT file.
Code:
Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
Dim finalLines As New List(Of String)
For Each line As String In lines
If Not line.EndsWith("|[email protected]") Then
finalLines.Add(line)
End If
Next
IO.File.WriteAllLines("Sources.txt", finalLines.ToArray())
End Sub
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|