2011-08-22 7 views
7

Desidero selezionare un semplice file .txt contenente righe di stringhe che utilizzano un controllo FileUpload. Ma invece di salvare effettivamente il file, voglio eseguire il ciclo su ciascuna riga di testo e visualizzare ogni riga in un controllo ListBox.Linee di trough di loop di file txt caricate tramite il controllo FileUpload

Esempio di un file di testo:

test.txt

123jhg345
182bdh774
473ypo433
129iiu454

Qual è il modo migliore per ottenere questo risultato?

Quello che ho finora:

private void populateListBox() 
{ 
    FileUpload fu = FileUpload1; 

    if (fu.HasFile) 
    { 
    //Loop trough txt file and add lines to ListBox1 
    } 
} 

risposta

14
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(); 
    } 
} 
+0

Grazie Shalini, Questo è quello che stavo cercando. Si noti che il lettore di stream doveva essere inizializzato come tale: ** StreamReader reader = new StreamReader (fu.PostedFile.InputStream); ** – PercivalMcGullicuddy

+0

Grazie amico. hai salvato la mia giornata –

4

aprire il file in un StreamReader e utilizzare


while(!reader.EndOfStream) 
{ 
    reader.ReadLine; 
    // do your stuff 
} 

Se volete sapere come ottenere il file/data in un corso d'acqua si prega di dire in quale forma si ottiene il file (s bytes)

8

Ecco un esempio di lavoro:

using (var file = new System.IO.StreamReader("c:\\test.txt")) 
{ 
    string line; 
    while ((line = file.ReadLine()) != null) 
    { 
     // do something awesome 
    } 
} 
3

Ci sono diversi modi per farlo, quelli sopra sono buoni esempi.

string line; 
string filePath = "c:\\test.txt"; 

if (File.Exists(filePath)) 
{ 
    // Read the file and display it line by line. 
    StreamReader file = new StreamReader(filePath); 
    while ((line = file.ReadLine()) != null) 
    { 
    listBox1.Add(line); 
    } 
    file.Close(); 
} 
0

C'è anche questo, utilizzando HttpPostedFileBase in MVC:

[HttpPost] 
public ActionResult UploadFile(HttpPostedFileBase file) 
{  
    if (file != null && file.ContentLength > 0) 
    { 
      //var fileName = Path.GetFileName(file.FileName); 
      //var path = Path.Combine(directory.ToString(), fileName); 
      //file.SaveAs(path); 
      var streamfile = new StreamReader(file.InputStream); 
      var streamline = string.Empty; 
      var counter = 1; 
      var createddate = DateTime.Now; 
      try 
      { 
       while ((streamline = streamfile.ReadLine()) != null) 
       { 
        //do whatever;// 
0
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.... 
     } 
    } 
} 
Problemi correlati