I am working on a program where users can upload a textfile with list of links, and after the upload is successful, the lines of the textfile is divided in two listbox where listbox1 contains 50% of the lines in textfile and listbox2 contains remaining 50% .
private void readFile()
{
int linenum = 1;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Text Files|*.txt";
openFileDialog1.Title = "Select a Text file";
openFileDialog1.FileName = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string file = openFileDialog1.FileName;
string[] text = System.IO.File.ReadAllLines(file);
foreach (string line in text)
{
if (linenum <= 150)
{
listBox1.Items.Add(line);
}
else
{
listBox2.Items.Add(line);
}
linenum++;
}
}
This code works fine when i know the exact numbers of line in text file, but it throws exception when the textfile consist less line. I am trying to divide the file in two equal parts and show it in two listbox. any advice or suggestion please.