简体   繁体   中英

Display random line from .txt file - c#

I'm trying to do something in c# (winforms) but i'm stuck on a small problem. I've tried all codes related to this problem but without success. Please read the problem before answering.

I have 2 functions. I want to make 1 function which will get a random line from specific .txt file and put it in the other one.

Here is an example of that:

//This is a ContexMenuStrip, a right click menu item that need to load Function1 (check the picture below

private void pdkName_Click(object sender, EventArgs e)
{
    Function1();
}

private void Function1()
{
      //CODE to Count and Display random line from .txt file
}

So far i've tried many codes that were previously posted here on stackoverflow.com and i've also tried tons of combinations with them. I will paste some of them here:


Random rand = new Random();
IEnumerable<string> lines = File.ReadLines(@"D:\FirstName.txt");
var lineToRead = rand.Next(1, lines.Count());
var line = lines.Skip(lineToRead - 1).First();

int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file =
    new System.IO.StreamReader(@"D:\FirstNames.txt");
while ((line = file.ReadLine()) != null)
{
    System.Console.WriteLine(line);
    counter++;
}
file.Close();
System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.
System.Console.ReadLine();

// This worked but only for the first line, can't do any combination with it (from the other functions to make it random)

using (StreamReader reader = File.OpenText(@"D:\FirstName.txt")
{
   textBox1.Text = reader.ReadLine();
}

var lines = File.ReadAllLines(@"D:\FirstNames.txt");
var r = new Random();
var randomLineNumber = r.Next(0, lines.Length - 1);
var line = lines[randomLineNumber];

string[] lines = File.ReadAllLines(@"D:\FirstNames.txt"); 
Random rand = new Random();
return lines[rand.Next(lines.Length)];

The function needs to count the file, pick a random line and return it. The item menu from the ContexMenuStrip that calls the function is used on a TEXTBOX.

So in general, i need a random name from the .txt file to be shown inside a textbox with clicking the right click on the textbox and selecting the item which loads my function. Here is a little picture with simple explanation.

在此处输入图片说明

Like fabian said or declare random inside the square brackets and directly calling the method next

string[] lines = File.ReadAllLines(@"C:\...\....\YourFile.txt");

textBox1.Text = lines[new Random().Next(lines.Length)];

define your random as a private member in your form:

private _rand = new Random();

Then in your Event from the ContextMenuStrip, paste this code (Make sure to edit the filename "yourFile.txt"):

var lines = File.ReadAllLines(@"D:\FirstNames.txt");
var randomLineNumber = _rand.Next(0, lines.Length - 1);
var line = lines[randomLineNumber]; //getting the random line
using (StreamWriter sw= File.AppendText("yourFile.txt")) 
{
     sw.WriteLine(line); //append the random line in your file
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM