I need to compare two text files by their properties in C#, and not by their content. For example, by date created and by date modified. I have managed to compare it by 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);
}
Good,
You must create a type object
DateTime
if you want to capture any of the dates you indicate and use one method of the classFile
or another. For example, for those who comment it would be like this:Capture last modified date:
Capture creation date:
I hope they can help you. All the best.
To obtain the information you require you must make use of the class
FileInfo
. I give you an example to obtain the creation date of a file: