Read the second line and save it with txt c #

I need to read only the second line in the .txt file and save it as a line so that I can use it later in the code.

The file name is "SourceSetting". Lines 1 and 2 have a few words

For line 1, I have this code:

string Location; StreamReader reader = new StreamReader("SourceSettings.txt"); { Location = reader.ReadLine(); } ofd.InitialDirectory = Location; 

And this works fine, but how can I make it so that it reads only the second line, so I can save it, for example:

 string Text 
+5
source share
5 answers

You can skip the first line without doing anything with it, so call ReadLine twice:

 string secondLine: using(var reader = new StreamReader("SourceSettings.txt")) { reader.ReadLine(); // skip secondLine = reader.ReadLine(); } 

Another way is the File class, which has convenient methods such as ReadLines :

 string secondLine = File.ReadLines("SourceSettings.txt").ElementAtOrDefault(1); 

Since ReadLines also uses a stream, the entire file must not first be loaded into memory to process it. Enumerable.ElementAtOrDefault will only accept the second row and not process more rows. If there are less than two lines, the result is null .

+11
source

Refresh I would advise you to go with Tim Schmelter's decision .

When you call ReadLine , it moves the cursor to the next line. So, in the second call, you will read the 2nd line.

 string Location; using(var reader = new StreamReader("SourceSettings.txt")) { Location = reader.ReadLine(); // this call will move caret to the begining of 2nd line. Text = reader.ReadLine(); //this call will read 2nd line from the file } ofd.InitialDirectory = Location; 

Do not forget about using .

Or an example of how to do this with the vi ReadLines of the File class if you only need one line from the file. But the solution with ElementAtOrDefault is the best like Tim Schmelter .

 var Text = File.ReadLines(@"C:\Projects\info.txt").Skip(1).First() 

The ReadLines and ReadAllLines methods differ as follows: when you use ReadLines, you can start enumerating a collection of strings before the entire collection is returned; when you use ReadAllLines, you must wait until the entire array of strings is returned before you can access the array. Therefore, when you work with very large files, ReadLines may be more efficient.

Thus, it does not read all the lines in memory compared to ReadAllLines .

+6
source

A line can be read using Linq as follows.

 var SecondLine = File.ReadAllLines("SourceSettings.txt").Skip(1).FirstOrDefault(); 
+2
source
 private string GetLine(string filePath, int line) { using (var sr = new StreamReader(filePath)) { for (int i = 1; i < line; i++) sr.ReadLine(); return sr.ReadLine(); } } 

Hope this helps :)

+2
source

If you know that your second line is unique because it contains a specific keyword that no longer appears in your file, you can also use linq, the advantage is that the second line can be any in the future.

  var myLine = File.ReadLines("SourceSettings.txt") .Where(line => line.Contains("The Keyword")) .ToList(); 
-1
source

All Articles