Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through lines of txt file uploaded via FileUpload control

I want to select a simple .txt file that contains lines of strings using a FileUpload control. But instead of actually saving the file I want to loop through each line of text and display each line in a ListBox control.

Example of a text file:

test.txt

123jhg345
182bdh774
473ypo433
129iiu454

What is the best way to accomplish this?

What I have so far:

private void populateListBox()
{
  FileUpload fu = FileUpload1;

  if (fu.HasFile)
  {
    //Loop trough txt file and add lines to ListBox1
   }
 }
like image 586
PercivalMcGullicuddy Avatar asked Aug 22 '11 15:08

PercivalMcGullicuddy


2 Answers

private void populateListBox()
{            
    List<string> tempListRecords = new List<string>();

    if (!FileUpload1.HasFile)
    {
        return;
    }
    using (StreamReader tempReader = new StreamReader(FileUpload1.FileContent))
    {
        string tempLine = string.Empty;
        while ((tempLine = tempReader.ReadLine()) != null)
        {
            // GET - line
            tempListRecords.Add(tempLine);
            // or do your coding.... 
        }
    }
}
like image 181
Roberto Mutti Avatar answered Nov 15 '22 19:11

Roberto Mutti


private void populateListBox() 
{
    FileUpload fu = FileUpload1; 
    if (fu.HasFile)  
    {
        StreamReader reader = new StreamReader(fu.FileContent);
        do
        {
            string textLine = reader.ReadLine();

            // do your coding 
            //Loop trough txt file and add lines to ListBox1  

        } while (reader.Peek() != -1);
        reader.Close();
    }
}
like image 45
62071072SP Avatar answered Nov 15 '22 21:11

62071072SP