Reading the single line from File randomly [Resolved]

Posted by Amatya under C# on 2/24/2016 | Points: 10 | Views : 1226 | Status : [Member] | Replies : 1
Good Morning Guys.. First I want to select the random number from 1 to 100, say I got 17 as a random number, then I want to read the 17 line from the notepad file and store into a string Variable.

Feel free to share informations.
mail Id '


Posted by: Rajnilari2015 on: 2/24/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50


@Amatya Sir,
You can try the below program

using System;

using System.IO;
using System.Linq;

namespace ConsoleApplication1
class Program
static void Main(string[] args)
string fileName = @"D:\Input.txt";

Console.WriteLine(File.ReadLines(fileName).ToList()[GetRandomNumbers(1, 20)]);

//generate random numbers
private static readonly Random random = new Random();
private static readonly object syncLock = new object();
private static int GetRandomNumbers(int minVal, int maxVal)
lock (syncLock) return random.Next(minVal, maxVal);

Hope this helps.
 Download source file

Thanks & Regards,
RNA Team

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response