Jobine23
Jobine23

Reputation: 130

Removing everything from a text file except first line

How can I remove everything from a text file except for the first line?

I tried:

var lines = File.ReadAllLines(path + "/resource.rpf" + "/" + "__resource.lua");
foreach (var item in lines.Take(1));

but doesn't work like I wanted.

Upvotes: 1

Views: 1060

Answers (2)

Racil Hilan
Racil Hilan

Reputation: 25351

lines is a string array, so you can simply use:

lines[0]

But it is better to only read the first line unless you want the other lines in some other parts of your code.

Upvotes: 1

Moffen
Moffen

Reputation: 2003

Using File.ReadAllLines

var lines = File.ReadAllLines(path + "/resource.rpf" + "/" + "__resource.lua");        
string firstLine = lines[0];

Using Stream Reader (best)

The best solution would be to only read in the first line to begin with:

using(StreamReader sr = new StreamReader(path" + "/resource.rpf" + "/" + "__resource.lua"))
{
    string firstLine = sr.ReadLine();
}

Upvotes: 4

Related Questions