views:

328

answers:

1

I have a Windows application that takes the data in the textboxes and writes them into a randomly generated text file, kinda keeping logs. Then there is this listbox that lists all these separate log files. The thing I want to do is to have another listbox display the file info of the selected one, the 2nd, 7th, 12th, ..., (2+5n)th lines of the text files that is selected after the button 'list info' is clicked. How is it possible to do this?

My code to update the first listbox is:

private void button2_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();

        DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Ece\Documents\Testings");
        // What type of file do we want?...

        FileInfo[] Files = dinfo.GetFiles("*.txt");
        // Iterate through each file, displaying only the name inside the listbox...

        foreach (FileInfo file in Files) 
        {
            listBox1.Items.Add(file.Name + "         " +file.CreationTime); 
        }

    }
+1  A: 

On the SelectedIndexChanged event you want to get the selected item. I wouldn't suggest showing the second part in another list box, but i'm sure you can work out how from the example below if you require it. I would personally have a richTextBox, and just read the file to there:

//Get the FileInfo from the ListBox Selected Item
FileInfo SelectedFileInfo = (FileInfo) listBox.SelectedItem;  

//Open a stream to read the file  
StreamReader FileRead = new StreamReader(SelectedFileInfo.FullName);

//Read the file to a string
string FileBuffer = FileRead.ReadToEnd();

//set the rich text boxes text to be the file
richTextBox.Text = FileBuffer;

//Close the stream so the file becomes free!
FileRead.Close();

Or if you are persistant to sticking with the ListBox then:

//Get the FileInfo from the ListBox Selected Item
FileInfo SelectedFileInfo = (FileInfo) listBox.SelectedItem;   

//Open a stream to read the file 
StreamReader FileRead = new StreamReader(SelectedFileInfo.FullName);

string CurrentLine = "";
int LineCount = 0;

//While it is not the end of the file
while(FileRead.Peek() != -1)
  {
  //Read a line
  CurrentLine = FileRead.ReadLine();

  //Keep track of the line count
  LineCount++;

  //if the line count fits your condition of 5n + 2
  if(LineCount % 5 == 2)
    {
    //add it to the second list box
    listBox2.Items.Add(CurrentLine);
    }
  }

//Close the stream so the file becomes free!
FileRead.Close();
ThePower
Not exactly what the poster was looking for, though I can't quite figure out WHAT the poster was looking for...
C. Ross
keeps saying 'System.String' type object couldnt be converted into 'System.IO.FileInfo' type.
Lady Sour
FileInfo SelectedFileInfo = (FileInfo) listBox1.SelectedItem; this one gives the error
Lady Sour
When you initially populate the ListBox with the files, do you add the items as FileInfo objects or as strings?
ThePower
If the ListBox with the file names contains FilePaths, you can swap that line with FileInfo SelectedFileInfo = new FileInfo(listBox1.SelectedItem);
ThePower