Advertisement
hiroyukims

arquivo c#

Jun 28th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace LendoTxtArmazenandoList
  6. {
  7.     class Program
  8.     {
  9.      
  10.         class Cliente
  11.         {
  12.             public string senha { get; set; }
  13.         }
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             //Instância da lista que será preenchida
  18.             List<Cliente> lista = new List<Cliente>();
  19.  
  20.             //Retorna todas as linhas do arquivo em um array
  21.             //de string, onde cada linha será um índice do array
  22.             string[] array = File.ReadAllLines(@"C:\exemplo.txt");
  23.  
  24.             //percorro o array e para cada linha
  25.             for (int i = 0; i < array.Length; i++)
  26.             {
  27.                 //crio um objeto do tipo Cliente
  28.                 Cliente c = new Cliente();
  29.  
  30.                 //Uso o método Split e quebro cada linha
  31.                 //em um novo array auxiliar, ou seja, cada
  32.                 //conteúdo do arquivo txt separado por '|' será
  33.                 //um nova linha neste array auxiliar. Assim sei que
  34.                 //cada índice representa uma propriedade
  35.                 string[] auxiliar = array[i].Split('|');
  36.  
  37.                 //Aqui recupero os itens, atribuindo
  38.                 //os mesmo as propriedade da classe
  39.                 //Cliente correspondentes, ou seja,
  40.                 //o índice zero será corresponde ao Id
  41.                 //o um ao nome e o dois ao e-mail
  42.                 c.Id = Convert.ToInt32(auxiliar[0]);
  43.                 c.Nome = auxiliar[1];
  44.                 c.Email = auxiliar[2];
  45.  
  46.                 //Adiciono o objeto a lista
  47.                 lista.Add(c);
  48.             }
  49.  
  50.             //Para verificar o resultado percorro a lista
  51.             //e exibo os valores recuparados pelo List<Cliente>
  52.             foreach (var item in lista)
  53.             {
  54.                 Console.WriteLine(@"Id: {0}; Nome: {1}; E-mail: {2};", item.Id, item.Nome, item.Email);
  55.                 Console.WriteLine(@"----------------------------------------------------------");
  56.             }
  57.  
  58.             Console.ReadKey();
  59.         }
  60.     }
  61. }
  62.  
  63. /*
  64. 1- 123456
  65. 2- 3456789
  66. 3- qwerty
  67. 4- 12345678
  68. 5- 111111
  69. 6- 1234567890
  70. 7- 1234567
  71. 8- password
  72. 9- 123123
  73. 10- 987654321
  74. 11- qwertyuiop
  75. 12- mynoob
  76. 13- 123321
  77. 14- 666666
  78. 15- 18atcskd2w
  79. 16- 7777777
  80. 17- 1q2w3e4r
  81. 18- 654321
  82. 19- 555555
  83. 20- 3rjs1la7qe
  84. 21- google
  85. 22- 1q2w3e4r5t
  86. 23- 123qwe
  87. 24- zxcvbnm
  88. 25- 1q2w3e
  89.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement