Skip to content

Instantly share code, notes, and snippets.

@parzibyte

parzibyte/csv.cs Secret

Created September 19, 2021 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/9033cae2bd0b0c4de55d6263126aeab8 to your computer and use it in GitHub Desktop.
Save parzibyte/9033cae2bd0b0c4de55d6263126aeab8 to your computer and use it in GitHub Desktop.
using System;
namespace LeerArchivoDeTextoCSharp
{
class Program
{
static void Main(string[] args)
{
/*
https://parzibyte.me/blog
*/
string ubicacionArchivo = "C:\\Users\\parzibyte\\Desktop\\productos.csv";
System.IO.StreamReader archivo = new System.IO.StreamReader(ubicacionArchivo);
string separador = ",";
string linea;
// Si el archivo no tiene encabezado, elimina la siguiente línea
archivo.ReadLine(); // Leer la primera línea pero descartarla porque es el encabezado
while ((linea = archivo.ReadLine()) != null)
{
string[] fila = linea.Split(separador);
string descripcion = fila[0];
double precio = Convert.ToDouble(fila[1]);
double existencia = Convert.ToDouble(fila[2]);
Console.WriteLine("Producto {0} con precio {1} y existencia {2}", descripcion, precio, existencia);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment