How to compare 2 files...?  
Author Message
Martin Kristensen





PostPosted: .NET Base Class Library, How to compare 2 files...? Top

Hello All of you,

I have a little problem. I working on a small project and in this moment I'm stuck. I need to see that 2 files have the same content, I know I have to use StreamReader and StreamWriter, but how do I do it easiest...

I hope you can help



.NET Development28  
 
 
nobugz





PostPosted: .NET Base Class Library, How to compare 2 files...? Top

Using FileStream and comparing the bytes in the file is better, it will work for any type of file, not just text files. Try this code:

public static bool FilesMatch(string path1, string path2) {
// Compares files <path1> and <path2>, returns true if their contents match exactly
byte[] buf1 = new byte[1024];
byte[] buf2 = new byte[1024];
using (FileStream f1 = new FileStream(path1, FileMode.Open, FileAccess.Read, FileShare.Read)) {
using (FileStream f2 = new FileStream(path2, FileMode.Open, FileAccess.Read, FileShare.Read)) {
for (; ; ) {
int cnt1 = f1.Read(buf1, 0, 1024);
int cnt2 = f2.Read(buf2, 0, 1024);
if (cnt1 != cnt2) return false;
for (int ix = 0; ix < cnt1; ++ix)
if (buf1[ix] != buf2[ix]) return false;
if (cnt1 < 1024) break;
}
}
}
return true;
}



 
 
Martin Kristensen





PostPosted: .NET Base Class Library, How to compare 2 files...? Top

Me and my friend are using Folding and a person in our class is changing the file so it is going to run as a service. Could you give an example to implant.... I don't see how to do it.... (I'm a noob yes.). And one more quastion do you know how to check every 30 second... I tried with itteration....but....., so do you have some references....
 
 
Sabrecat





PostPosted: .NET Base Class Library, How to compare 2 files...? Top

And one more quastion do you know how to check every 30 second... I tried with itteration....but....., so do you have some references....

I use somthing similar to this in my windows service. canContinue is a flag that I setup to be set to false when the service gets the message to terminate.

while ( canContinue )

{

do_processing ();

Thread.Sleep(30000);

}

Simplistic. But it works for me.


 
 
Martin Kristensen





PostPosted: .NET Base Class Library, How to compare 2 files...? Top

using System;

using System.Collections;

using System.ComponentModel;

using System.Configuration.Install;

using System.IO;

using System.ServiceProcess;

using System.Threading;

public class AVCheck : System.ServiceProcess.ServiceBase

{

public const string SOURCE_FILE = "MyFolding.txt";

public const string DEST_FILE = "client.cfg";

public AVCheck()

{

this.AutoLog = true;

this.CanPauseAndContinue = true;

this.CanStop = true;

this.ServiceName = "AVCheck";

}

public static bool FilesMatch(string path1, string path2)

{

// Compares files <path1> and <path2>, returns true if their contents match exactly

byte[] buf1 = new byte[1024];

byte[] buf2 = new byte[1024];

using (FileStream f1 = new FileStream(path1, FileMode.Open, FileAccess.Read, FileShare.Read))

{

using (FileStream f2 = new FileStream(path2, FileMode.Open, FileAccess.Read, FileShare.Read))

{

for (; ; )

{

int cnt1 = f1.Read(buf1, 0, 1024);

int cnt2 = f2.Read(buf2, 0, 1024);

if (cnt1 != cnt2) return false;

for (int ix = 0; ix < cnt1; ++ix)

if (buf1[ix] != buf2[ix]) return false;

if (cnt1 < 1024) break;

}

}

}

return true;

}

public static void Main()

{

System.ServiceProcess.ServiceBase.Run(new AVCheck());

}

protected override void OnStart(string[] args)

{

}

protected override void OnStop()

{

}

}

[RunInstallerAttribute(true)]

public class MyProjectInstaller : Installer

{

private ServiceInstaller serviceInstaller;

private ServiceProcessInstaller processInstaller;

public MyProjectInstaller()

{

processInstaller = new ServiceProcessInstaller();

processInstaller.Account = ServiceAccount.LocalSystem;

serviceInstaller = new ServiceInstaller();

serviceInstaller.StartType = ServiceStartMode.Automatic;

serviceInstaller.ServiceName = "AVCheck";

Installers.Add(serviceInstaller);

Installers.Add(processInstaller);

}

}

This is the source code, I have at this time.... canContinue is going to be set in the constructor in this case.... this.canContinue = false;, but it don't reconizes the code :S


 
 
WhiteAndNerdy





PostPosted: .NET Base Class Library, How to compare 2 files...? Top

If you dont need to show the differences, you could just hash the two files and then compare the two output hashes, that would tell you if they were different.

Just as easy to implement too