lets say i have 5 lines within a txt file called users.txt each line has the following information
username:password
how would i go about spliting each line within a txt file and store the username as one string and password as the other.
I have the code to grab a random line using this code. This code is used for another part of my project aswell so I dont want the code to be altered. I was thinking after the line has been grabbed call another function but I have no idea on how to split it with the :
private static string GetRandomLine(string file)
{
List<string> lines = new List<string>();
Random rnd = new Random();
int i = 0;
try
{
if (File.Exists(file))
{
//StreamReader to read our file
StreamReader reader = new StreamReader(file);
//Now we loop through each line of our text file
//adding each line to our list
while (!(reader.Peek() == -1))
lines.Add(reader.ReadLine());
//Now we need a random number
i = rnd.Next(lines.Count);
//Close our StreamReader
reader.Close();
//Dispose of the instance
reader.Dispose();
//Now write out the random line to the TextBox
return lines[i].Trim();
}
else
{
//file doesn't exist so return nothing
return string.Empty;
}
}
catch (IOException ex)
{
MessageBox.Show("Error: " + ex.Message);
return string.Empty;
}
}