Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 15, 2015 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/f2c4387ea99fb3474224 to your computer and use it in GitHub Desktop.
Save Fhernd/f2c4387ea99fb3474224 to your computer and use it in GitHub Desktop.
Comparación de dos archivos en C#.
// OrtizOL - xCSw - http://ortizol.blogspot.com
using System;
using System.IO;
using System.Security.Cryptography;
namespace Receta.CSharp.R0511
{
public class CompararArchivos
{
public static void Main(string[] args)
{
Console.WriteLine(Environment.NewLine);
// Valida que el usuario haya ingresado el número correcto
// de argumentos desde la línea de comandos:
if (args.Length != 2)
{
Console.WriteLine("Instrucciones: CompararArchivos.exe [NombreArchvo] [NombreArchivo]");
return;
}
Console.WriteLine("Comparación de {0} y {1}", args[0], args[1]);
// Creación de un objeto HashAlgorithm:
using (HashAlgorithm hash = HashAlgorithm.Create())
{
using (FileStream fs1 = new FileStream(args[0], FileMode.Open),
fs2 = new FileStream(args[1], FileMode.Open))
{
// Cálculo de hash para los dos archivos:
byte[] hashBytes1 = hash.ComputeHash(fs1);
byte[] hashBytes2 = hash.ComputeHash(fs2);
// Comparación de los dos códigos hash:
if (BitConverter.ToString(hashBytes1) == BitConverter.ToString(hashBytes2))
{
Console.WriteLine("\nLos archivos son iguales.");
}
else
{
Console.WriteLine("\nLos archivos no son iguales.");
}
}
}
Console.WriteLine(Environment.NewLine);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment