2011-08-22 24 views
7

Quiero seleccionar un archivo .txt simple que contiene líneas de cadenas utilizando un control FileUpload. Pero en lugar de guardar el archivo, quiero recorrer cada línea de texto y mostrar cada línea en un control ListBox.Líneas a través de bucle de archivo txt cargadas mediante el control FileUpload

Ejemplo de un archivo de texto:

test.txt

123jhg345
182bdh774
473ypo433
129iiu454

¿Cuál es la mejor manera de lograr esto?

Lo que tengo hasta ahora:

private void populateListBox() 
{ 
    FileUpload fu = FileUpload1; 

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

Respuesta

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

Gracias Shalini, Esto es lo que estaba buscando. Tenga en cuenta que el lector de flujo debe ser inicializado como tal: ** Lector StreamReader = nuevo StreamReader (fu.PostedFile.InputStream); ** – PercivalMcGullicuddy

+0

Gracias amigo. Salvaste mi día. –

4

abrir el archivo en un StreamReader y utilizar


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

Si quieres saber cómo conseguir el archivo/día en una corriente favor decir en ¿qué forma se obtiene el archivo (s bytes)

8

Aquí está un ejemplo de trabajo:

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

Hay algunas maneras diferentes de hacerlo, las de arriba son buenos ejemplos.

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

No es esto también, el uso de HttpPostedFileBase en 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.... 
     } 
    } 
} 
Cuestiones relacionadas