C # compare two files by their properties

0

I need to compare two text files for their properties in C #, and not for their content. For example, by creation date and by modification date. I have managed to compare it by the content, I leave here the code:

private bool FileCompare(string file1, string file2)
{
     int file1byte;
     int file2byte;
     FileStream fs1;
     FileStream fs2;

     if (file1 == file2)
     {
          return true;
     }

     fs1 = new FileStream(file1, FileMode.Open);
     fs2 = new FileStream(file2, FileMode.Open);

     if (fs1.Length != fs2.Length)
     {
          // Close the file
          fs1.Close();
          fs2.Close();

          return false;
     }

     do 
     {
          // Read one byte from each file.
          file1byte = fs1.ReadByte();
          file2byte = fs2.ReadByte();
     }
     while ((file1byte == file2byte) && (file1byte != -1));

     fs1.Close();
     fs2.Close();

     return ((file1byte - file2byte) == 0);
}
    
asked by LopezAi 28.09.2017 в 09:34
source

2 answers

3

Good morning,

You must create an object type DateTime if you want to capture some of the dates you indicate and use a method of class File or other. For example, for those who comment would be like this:

Capture date of last modification:

DateTime lastModif = File.GetLastWriteTime(fileName);
DateTime lastModif2 = File.GetLastWriteTime(fileName2);

Capture creation date:

DateTime creationTime = File.GetCreationTime(fileName);
DateTime creationTime2 = File.GetCreationTime(fileName2);

I hope you will be helped. Greetings.

    
answered by 28.09.2017 / 09:45
source
2

To obtain the information you need, you must use the class FileInfo . I give you an example to obtain the date of creation of a file:

FileInfo fi = new FileInfo(@"c:\prueba.txt");
DateTime fechaCreacion = fi.CreationTime;
    
answered by 28.09.2017 в 09:44