Programa De Comparación De Archivos En C#

Este programa de C# compara dos archivos de cualquier tipo (texto, binario, etc.) para determinar si son idénticos. El programa lee el contenido de ambos archivos y comprueba si cada byte del primero coincide con el byte correspondiente del segundo. Si todos los bytes son iguales, los archivos se consideran idénticos; de lo contrario, son diferentes. El programa proporciona información al usuario indicando si los archivos coinciden o no.

Grupo

Manejo de archivos en C#

Objectivo

1. El programa espera dos rutas de archivo como argumentos de la línea de comandos.
2. Comparará los archivos byte a byte para comprobar si son idénticos.
3. Si los archivos son idénticos, el programa informará al usuario de que coinciden.
4. Si los archivos son diferentes, el programa informará al usuario y mostrará la posición del byte donde se produjo la discrepancia.

Ejemplo de uso:

compareFiles file1.txt file2.txt


Crear un programa en C# para determinar si dos archivos (de cualquier tipo) son idénticos (tienen el mismo contenido).

Ejemplo de ejercicio en C#

 Copiar código C#
using System;
using System.IO;

class FileComparison
{
    // Main method where the program starts execution
    static void Main(string[] args)
    {
        // Check if two file paths have been provided as arguments
        if (args.Length != 2)
        {
            Console.WriteLine("Usage: compareFiles  ");
            return;
        }

        // Get the file paths from the command line arguments
        string file1 = args[0];
        string file2 = args[1];

        // Check if both files exist
        if (!File.Exists(file1))
        {
            Console.WriteLine($"The file {file1} does not exist.");
            return;
        }

        if (!File.Exists(file2))
        {
            Console.WriteLine($"The file {file2} does not exist.");
            return;
        }

        // Open the files for reading
        using (FileStream fs1 = new FileStream(file1, FileMode.Open, FileAccess.Read),
                          fs2 = new FileStream(file2, FileMode.Open, FileAccess.Read))
        {
            // Check if the files have the same length
            if (fs1.Length != fs2.Length)
            {
                Console.WriteLine("The files are not identical (different sizes).");
                return;
            }

            // Compare the files byte by byte
            bool filesAreIdentical = true;
            int bytePosition = 0;
            while (fs1.Position < fs1.Length)
            {
                // Read a byte from both files
                int byte1 = fs1.ReadByte();
                int byte2 = fs2.ReadByte();

                // Check if the bytes are the same
                if (byte1 != byte2)
                {
                    filesAreIdentical = false;
                    Console.WriteLine($"Files differ at byte position {bytePosition}. File1 has byte {byte1}, File2 has byte {byte2}.");
                    break;
                }

                bytePosition++;
            }

            // Inform the user if the files are identical
            if (filesAreIdentical)
            {
                Console.WriteLine("The files are identical.");
            }
        }
    }
}

 Output

//Case 1 - Files are identical:

//For the command:
compareFiles file1.txt file2.txt

//Output:
The files are identical.

//Case 2 - Files are different:

//For the command:
compareFiles file1.txt file2.txt

//Output (if the files differ at byte position 100):
Files differ at byte position 100. File1 has byte 65, File2 has byte 66.

Comparte este ejercicio de C#