ggddhhii

weaaaaaa

Jul 30th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace _7_Archivos
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {        
  11.             string path = "c:\\temp\\myTest.txt";
  12.  
  13.             try
  14.             {
  15.                 //verifica la existencia del archivo
  16.                 if(File.Exists(path))
  17.                 {//si existe lo elimina
  18.                     Console.WriteLine("si existe");
  19.                     File.Delete(path);
  20.                 }
  21.  
  22.                 //escritura de archivo
  23.                 //using (FileStream fs = File.Create(path))
  24.                 //{
  25.                 //    byte[] info = new UTF8Encoding(true).GetBytes("hola mundo");
  26.  
  27.                 //        //byte[], offset, count
  28.                 //    fs.Write(info, 0, info.Length);//esta linea guarda
  29.                 //}
  30.  
  31.                
  32.                
  33.                 //escritura de archivo
  34.                 using (StreamWriter sw = File.CreateText(path))
  35.                 {
  36.                     //byte[] info = new UTF8Encoding(true).GetBytes("hola mundo");
  37.  
  38.                     //byte[], offset, count
  39.                     sw.WriteLine("hola mundo");
  40.                     sw.WriteLine("como estas?");
  41.                     sw.WriteLine("hola mundo");
  42.                     sw.WriteLine("como estas?");
  43.                     sw.WriteLine("hola mundo");
  44.                     sw.WriteLine("como estas?");
  45.                     sw.WriteLine("hola mundo");
  46.                     sw.WriteLine("como estas?");
  47.                     sw.WriteLine("hola mundo");
  48.                     sw.WriteLine("como estas?");
  49.                 }
  50.  
  51.  
  52.                 //lectura de archivo
  53.                 using (StreamReader sr = File.OpenText(path))
  54.                 {
  55.                     string linea = "";
  56.  
  57.                     //barrido del archivo
  58.                     while ((linea = sr.ReadLine()) != null)
  59.                     {
  60.                         Console.WriteLine(linea);
  61.                     }
  62.                 }
  63.             }
  64.             catch (Exception ex)
  65.             {
  66.                 Console.WriteLine(ex.ToString());
  67.             }
  68.             Console.ReadLine();
  69.  
  70.            
  71.  
  72.  
  73.         }
  74.     }
  75. }
Add Comment
Please, Sign In to add comment