user3542613 user3542613 - 1 year ago 191
C# Question

C# read only first line from a text file

so what I'm failing to do is, MyFile.txt has either "english", "french" or "german" in the first line
and I want to get the language from the first line of the text file, then continue my code

String[] languages = new String[] { "english", "french", "german"};

foreach (String language in languages)
string line1 = File.ReadLines("MyFile.txt").Skip(0).Take(1);
line1 = language;

Answer Source

You probably want something like this:

String[] languages = new String[] { "english", "french", "german"};

string line1 = File.ReadLines("MyFile.txt").First(); // gets the first line from file.
if(languages.Contains(line1)) // check if first line matches specified languages
    // success
    Console.WriteLine("Language is {0}", line1);
    // fail
    Console.WriteLine("Language not found: {0}", line1);    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download