Okay, as promised, I will help you now.
Create a console application, you have no need of Windows Forms for something so simple. Note that because a console application's main() method is static (Actually, all application's main method is static, if you look in Program.cs) you will need to declare any globally scoped variables in that file as static if they are referred to by main.
For this, the easiest way will be to use an array of strings, and the easiest way to make an array is with the ArrayList class, as you can just use "ArrayList.Add()" rather than going through all of this shit just to resize one little array.
Add these includes:
Code:
using System.IO;
using System.Collections;
System.IO contains the StreamReader class which is useful for reading the contents of files. System.Collections contains the ArrayList class which I explained earlier.
Inside your class, create a StreamReader variable in main() like so:
Code:
StreamReader fileReader = new StreamReader(@"C:\test\test.txt");
In this instance, I will be using C:\test\test.txt as the file I am reading. Alter the path as you wish. If you want to create a globally scoped StreamReader instance, by all means use this code:
Code:
static StreamReader fileReader;
And initialize it in main() like so:
Code:
fileReader = new StreamReader(@"path:\to\file");
The StreamReader class provides many ways to read a file. If you don't know how Streams work, I will tell you the basics of using a Stream reader. Notice there is a class member called "StreamReader.EndOfStream". This is a boolean which tells you whether or not you have reached the end of the stream, and yields true once you have reached the end. This is ideal for looping through the file if you're going to access the file more than once. An ideal way of doing this would be with a
while or
do-while loop. For example:
Code:
while(!fileReader.EndOfStream)
{
// Insert file reading code, such as fileReader.ReadLine()
}
There is of course, a StreamReader.ReadToEnd() method available, but for the purpose of adding each individual line to an array, reading each line separately of course makes more sense.
Now, before you go and read from the file, you will want an array to store the data in! Simply, you can use the ArrayList class, part of the System.Collections namespace, which is ideal for this purpose as mentioned earlier, because you can just use the ArrayList.Add(data) method to add each line. Declare an ArrayList like so:
Code:
ArrayList fileData = new ArrayList();
Now, to add each line to ArrayList, all you need to do is loop through the stream and do the following:
Code:
fileData.Add(fileReader.ReadLine());
Hint: Remember the EndOfStream variable I discussed earlier?
The other excellent thing about ArrayLists, is that you can access each element like a normal array. For example, I can simply access the first element of an ArrayList by using ArrayList[0]. To access a random line, you will need to generate a random number and access an element of the ArrayList using that number.
Random numbers can be a little confusing. You actually need to seed the random number generator before you generate, otherwise you will get the same output every time you start your program. To do this, a common method is to get some form of current time. In this example, I'll use the DateTime class, and use the amount of milliseconds that have elapsed for today as my seed. In languages like C++, you used to have to seed a random function by using srand(), but with C#'s Random() class, you can do this in the initializer, like so:
Code:
Random randomLineGenerator = new Random(DateTime.Now.Millisecond);
And to generate a random number to access your array with, the following:
Code:
int randomIndex = randomLineGenerator.Next(fileData.Count);
Now, remember how I said you can access an ArrayList as a normal array? All you have to do is print the line and you're done!
Code:
Console.WriteLine("Line {0}: {1}", randomIndex, fileData[randomIndex]);
Console.Write("\nPress any key to continue ...");
Console.ReadKey();
And that my friend, is how you access a random line of a file. There are probably more efficient ways of doing such, but this is definitely a simple one.