Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- namespace _7_Archivos
- {
- class Program
- {
- static void Main(string[] args)
- {
- string path = "c:\\temp\\myTest.txt";
- try
- {
- //verifica la existencia del archivo
- if(File.Exists(path))
- {//si existe lo elimina
- Console.WriteLine("si existe");
- File.Delete(path);
- }
- //escritura de archivo
- //using (FileStream fs = File.Create(path))
- //{
- // byte[] info = new UTF8Encoding(true).GetBytes("hola mundo");
- // //byte[], offset, count
- // fs.Write(info, 0, info.Length);//esta linea guarda
- //}
- //escritura de archivo
- using (StreamWriter sw = File.CreateText(path))
- {
- //byte[] info = new UTF8Encoding(true).GetBytes("hola mundo");
- //byte[], offset, count
- sw.WriteLine("hola mundo");
- sw.WriteLine("como estas?");
- sw.WriteLine("hola mundo");
- sw.WriteLine("como estas?");
- sw.WriteLine("hola mundo");
- sw.WriteLine("como estas?");
- sw.WriteLine("hola mundo");
- sw.WriteLine("como estas?");
- sw.WriteLine("hola mundo");
- sw.WriteLine("como estas?");
- }
- //lectura de archivo
- using (StreamReader sr = File.OpenText(path))
- {
- string linea = "";
- //barrido del archivo
- while ((linea = sr.ReadLine()) != null)
- {
- Console.WriteLine(linea);
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- Console.ReadLine();
- }
- }
- }
Add Comment
Please, Sign In to add comment