Buenas tardes, escribo en esta oportunidad porque tengo que leer archivo con formato txt, csv y excel todo dependiendo de la opción que eliga el usuario en subir el archivo para leerlo, estaba utilizando este código pero me da error, que hice malo?
List<Notificacion> lista = new List<Notificacion>();
respuesta="";
string folderName = "Descargas";
string webRootPath = _hostingEnvironment.WebRootPath;
string newPath = Path.Combine(webRootPath, folderName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
if (file.Length > 0)
{
string fullPath = Path.Combine(newPath, file.FileName);
string sFileExtension = Path.GetExtension(file.FileName).ToLower();
using (var stream = new FileStream(fullPath, FileMode.Create))
{
file.CopyTo(stream);
stream.Position = 0;
stream.Close(); //close text fi
}
if (sFileExtension == TypeFile.ArchivoTXT || sFileExtension == TypeFile.ArchivoCSV)
{
lista = LeerArchivoTxtCsv(fullPath, detLectura.ConDelimitador);
}
}
else
{
respuesta = "El archivo esta vacío";
}
return lista;
public List<Notificacion> LeerArchivoTxtCsv(string filename, string separacion)
{
List<Notificacion> lista = new List<Notificacion>();
var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
while (!streamReader.EndOfStream)
{
string item = streamReader.ReadLine();
if (!String.IsNullOrWhiteSpace(item))
{
List<string> lectura = formatCelda(item.Split(separacion).ToList());
if(!String.IsNullOrWhiteSpace(lectura.ElementAtOrDefault(ColDescripcion).ToString()) && !lectura.ElementAtOrDefault(ColDescripcion).ToString().ToLower().Contains(descripcion.ToLower()))
{
if (!String.IsNullOrWhiteSpace(lectura.ElementAtOrDefault(ColMonto).ToString()))
{
if(FuncionesGlobales.convertStringDecimal(lectura.ElementAtOrDefault(ColMonto)) > 0)
{
lista.Add(new NotificacionPago
{
NpNroReferencia = lectura.ElementAtOrDefault(ColReferencia).ToString(),
NpMonto = FuncionesGlobales.convertStringDecimal(lectura.ElementAtOrDefault(ColMonto).ToString())
});
}
}
}
}
}
}
fileStream.Close();
return lista;
}
Me sale 2 errores que me da sale, es que otro proceso lo esta utilizando, lo que no entiendo es que en todas cierro las conexiones, que puedo tener malo en el código? y el otro error que me sale es:
Object reference not set to an instance of an object