Created
July 15, 2015 11:44
-
-
Save Fhernd/f2c4387ea99fb3474224 to your computer and use it in GitHub Desktop.
Comparación de dos archivos en C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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