mardi 21 août 2018

read a random line from a file

I'd like to put all the lines from my text file into a list and then select a random line to print. Is this possible? I have found a way to read all the lines in the file, but i can't find a way to print a specific line. preferably i'd just generate a random number and then print the corresponding line.

string[] lines = System.IO.File.ReadAllLines(@"test.txt");
        System.Console.WriteLine("Contents of test.txt = ");
        Random rd = new Random();
        int ball = rd.Next(0, 75);

        foreach (string e in lines)
        {
           Console.WriteLine(e);
        }

        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }




Aucun commentaire:

Enregistrer un commentaire