Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace LendoTxtArmazenandoList
- {
- class Program
- {
- class Cliente
- {
- public string senha { get; set; }
- }
- static void Main(string[] args)
- {
- //Instância da lista que será preenchida
- List<Cliente> lista = new List<Cliente>();
- //Retorna todas as linhas do arquivo em um array
- //de string, onde cada linha será um índice do array
- string[] array = File.ReadAllLines(@"C:\exemplo.txt");
- //percorro o array e para cada linha
- for (int i = 0; i < array.Length; i++)
- {
- //crio um objeto do tipo Cliente
- Cliente c = new Cliente();
- //Uso o método Split e quebro cada linha
- //em um novo array auxiliar, ou seja, cada
- //conteúdo do arquivo txt separado por '|' será
- //um nova linha neste array auxiliar. Assim sei que
- //cada índice representa uma propriedade
- string[] auxiliar = array[i].Split('|');
- //Aqui recupero os itens, atribuindo
- //os mesmo as propriedade da classe
- //Cliente correspondentes, ou seja,
- //o índice zero será corresponde ao Id
- //o um ao nome e o dois ao e-mail
- c.Id = Convert.ToInt32(auxiliar[0]);
- c.Nome = auxiliar[1];
- c.Email = auxiliar[2];
- //Adiciono o objeto a lista
- lista.Add(c);
- }
- //Para verificar o resultado percorro a lista
- //e exibo os valores recuparados pelo List<Cliente>
- foreach (var item in lista)
- {
- Console.WriteLine(@"Id: {0}; Nome: {1}; E-mail: {2};", item.Id, item.Nome, item.Email);
- Console.WriteLine(@"----------------------------------------------------------");
- }
- Console.ReadKey();
- }
- }
- }
- /*
- 1- 123456
- 2- 3456789
- 3- qwerty
- 4- 12345678
- 5- 111111
- 6- 1234567890
- 7- 1234567
- 8- password
- 9- 123123
- 10- 987654321
- 11- qwertyuiop
- 12- mynoob
- 13- 123321
- 14- 666666
- 15- 18atcskd2w
- 16- 7777777
- 17- 1q2w3e4r
- 18- 654321
- 19- 555555
- 20- 3rjs1la7qe
- 21- google
- 22- 1q2w3e4r5t
- 23- 123qwe
- 24- zxcvbnm
- 25- 1q2w3e
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement