Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // OrtizOL - xCSw - http://ortizol.blogspot.com
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6.  
  7. namespace Receta.CSharp.R0511
  8. {
  9.     public class CompararArchivos
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             Console.WriteLine(Environment.NewLine);
  14.            
  15.             // Valida que el usuario haya ingresado el número correcto
  16.             // de argumentos desde la línea de comandos:
  17.             if (args.Length != 2)
  18.             {
  19.                 Console.WriteLine("Instrucciones: CompararArchivos.exe [NombreArchvo] [NombreArchivo]");
  20.                 return;
  21.             }
  22.            
  23.             Console.WriteLine("Comparación de {0} y {1}", args[0], args[1]);
  24.            
  25.             // Creación de un objeto HashAlgorithm:
  26.             using (HashAlgorithm hash = HashAlgorithm.Create())
  27.             {
  28.                 using (FileStream fs1 = new FileStream(args[0], FileMode.Open),
  29.                                   fs2 = new FileStream(args[1], FileMode.Open))
  30.                 {
  31.                     // Cálculo de hash para los dos archivos:
  32.                     byte[] hashBytes1 = hash.ComputeHash(fs1);
  33.                     byte[] hashBytes2 = hash.ComputeHash(fs2);
  34.                    
  35.                     // Comparación de los dos códigos hash:
  36.                     if (BitConverter.ToString(hashBytes1) == BitConverter.ToString(hashBytes2))
  37.                     {
  38.                         Console.WriteLine("\nLos archivos son iguales.");
  39.                     }
  40.                     else
  41.                     {
  42.                         Console.WriteLine("\nLos archivos no son iguales.");
  43.                     }
  44.                 }
  45.             }
  46.            
  47.             Console.WriteLine(Environment.NewLine);
  48.         }
  49.     }
  50. }