Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp


File comparer - Practice Exercises C# Sharp


Lesson 8:

File management


Exercise 8.31:

File comparer


Objetive:

Create a C# program to tell if two files (of any kind) are identical (have the same content).


Source Code:


using System;
using System.IO;
namespace FileComparer
{
    class Program
    {
        static void Main(string[] args)
        {
            bool equal = true;

            FileStream myFile1;
            byte[] dataFile1;
            FileStream myFile2;
            byte[] dataFile2;

            Console.Write("Enter the name of file1: ");
            string fileName1 = Console.ReadLine();

            Console.Write("Enter the name of file2: ");
            string fileName2 = Console.ReadLine();

            if ((!File.Exists(fileName1)) || (!File.Exists(fileName2)))
            {
                Console.WriteLine("The file 1 or file 2 not exists!!!");
                return;
            }

            try
            {
                myFile1 = File.OpenRead(fileName1);
                dataFile1 = new byte[myFile1.Length];
                myFile1.Read(dataFile1, 0, (int)myFile1.Length);
                myFile1.Close();

                myFile2 = File.OpenRead(fileName2);
                dataFile2 = new byte[myFile2.Length];
                myFile2.Read(dataFile2, 0, (int)myFile2.Length);
                myFile2.Close();

                if (myFile1.Length == myFile2.Length)
                    for (int i = 0; i < dataFile1.Length; i++)
                        if (dataFile1[i] != dataFile2[i])
                            equal = false;
                        else
                            equal = false;

                if (equal)
                    Console.WriteLine("The {0} is equal {1}", fileName1, fileName2);
                else
                    Console.WriteLine("The {0} not is equal {1}", fileName1, fileName2);

                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}!!!", e.Message);
            }
        }
    }
}
Exercisey 8.31




Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.