'c# File.ReadAllLines doesn't read blank lines
I am using c# for reading some txt documents and writing/deleting some lines in them I have to read a text document, delete some of it's lines and then save it again in the same path. It worked all good until I realized that it doesn't read empty lines or when there is just a space in a new line :(.
Here's my text file structure:
-Name
-LastName
-Age
-Phone
-Address
-Address2(optional) -// this line will be deleted
-Address3(optional) -// this line will be deleted
****here comes the empty line****
Here's my code:
List<string> myLines = File.ReadAllLines(path).ToList();
if (myLines.Count > 5)
{
for(int i = 7; i >= 5; i--)
{
myLines.RemoveAt(i);
}
File.WriteAllLines(path, myLines.ToArray());
}
So I don't know why when I run File.ReadAllLines will give me 7 lines (ignoring the blank one) and of course after I delete something in the file, the blank line is still there.
Note: I am working with more than 100k files, either way I would just delete that specific line by hand.
Can you help me sort this out and delete that blank line? Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
