Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Now, I want to remove the string in the .txt file that the user used. This is to make it invalid for use.


Thanks to Tim Schmelter for the corrected code.

If .txt file.Contains(stN) Then
    'Do anything here
    'Then I want to remove the string used.
End If
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
239 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have to rewrite the entire file:

Dim newLines = File.ReadAllLines(path).
    .Where(Function(l) Not l.Trim.Equals(stN, StringComparison.OrdinalIgnoreCase) )
File.WriteAllLines(path, newLines)

If you don't want to use Trim and the case insensitive comparison:

.Where(Function(l) l <> stN)

Edit: Are you using .NET 3.5? Then File.WriteAllLines does not accept an IEnumerable(Of String) but only String(). You need to create one from the query:

File.WriteAllLInes(path, newLines.ToArray())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...