Skip to main content
added 110 characters in body
Source Link
Daniel Peñalba
  • 32.1k
  • 36
  • 146
  • 226

Well, I'm not sure if you can in the file write timestamps. If not, your unique alternative, is comparing the content of the files. 

A fast way to do thatsimple approach is calculatingcomparing the files byte-to-byte, but if you're going to compare a file several times with others, you can calculate the hashcode of the file contentsfiles and compare it. 

The following code snippet shows how toyou can do it.:

 public static string CalcHashCode(string filename) { FileStream stream = new FileStream( filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); try { return CalcHashCode(stream); } finally { stream.Close(); } } public static string CalcHashCode(FileStream file) { MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider(); Byte[] hash = md5Provider.ComputeHash(file); return Convert.ToBase64String(hash); } 

EDIT: If you're going to compare a file with others more that one time, you can save the file hash and compare it. For a single comparison, the byte-to-byte comparison is better. You need also to recompute hash when the file changes, but if you're going to do massive comparisons (more than one time), I recommend using the hash approach.

Well, I'm not sure if you can in the file write timestamps. If not, your unique alternative, is comparing the content of the files. A fast way to do that is calculating the hashcode of the file contents. The following code snippet shows how to do it.

 public static string CalcHashCode(string filename) { FileStream stream = new FileStream( filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); try { return CalcHashCode(stream); } finally { stream.Close(); } } public static string CalcHashCode(FileStream file) { MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider(); Byte[] hash = md5Provider.ComputeHash(file); return Convert.ToBase64String(hash); } 

EDIT: If you're going to compare a file with others more that one time, you can save the file hash and compare it. For a single comparison, the byte-to-byte comparison is better. You need also to recompute hash when the file changes, but if you're going to do massive comparisons (more than one time), I recommend using the hash approach.

Well, I'm not sure if you can in the file write timestamps. If not, your unique alternative, is comparing the content of the files. 

A simple approach is comparing the files byte-to-byte, but if you're going to compare a file several times with others, you can calculate the hashcode of the files and compare it. 

The following code snippet shows how you can do it:

 public static string CalcHashCode(string filename) { FileStream stream = new FileStream( filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); try { return CalcHashCode(stream); } finally { stream.Close(); } } public static string CalcHashCode(FileStream file) { MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider(); Byte[] hash = md5Provider.ComputeHash(file); return Convert.ToBase64String(hash); } 

If you're going to compare a file with others more that one time, you can save the file hash and compare it. For a single comparison, the byte-to-byte comparison is better. You need also to recompute hash when the file changes, but if you're going to do massive comparisons (more than one time), I recommend using the hash approach.

added 186 characters in body
Source Link
Daniel Peñalba
  • 32.1k
  • 36
  • 146
  • 226

Well, I'm not sure if you can in the file write timestamps. If not, your unique alternative, is comparing the content of the files. A fast way to do that is calculating the hashcode of the file contents. The following code snippet shows how to do it.

 public static string CalcHashCode(string filename) { FileStream stream = new FileStream( filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); try { return CalcHashCode(stream); } finally { stream.Close(); } } public static string CalcHashCode(FileStream file) { MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider(); Byte[] hash = md5Provider.ComputeHash(file); return Convert.ToBase64String(hash); } 

EDIT: If you're going to compare a file with others more that one time, you can save the file hash and compare it. For a single comparison, the byte-to-byte comparison is better. You need also to recompute hash when the file changes, but if you're going to do massive comparisons (more than one time), I recommend using the hash approach.

Well, I'm not sure if you can in the file write timestamps. If not, your unique alternative, is comparing the content of the files. A fast way to do that is calculating the hashcode of the file contents. The following code snippet shows how to do it.

 public static string CalcHashCode(string filename) { FileStream stream = new FileStream( filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); try { return CalcHashCode(stream); } finally { stream.Close(); } } public static string CalcHashCode(FileStream file) { MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider(); Byte[] hash = md5Provider.ComputeHash(file); return Convert.ToBase64String(hash); } 

Well, I'm not sure if you can in the file write timestamps. If not, your unique alternative, is comparing the content of the files. A fast way to do that is calculating the hashcode of the file contents. The following code snippet shows how to do it.

 public static string CalcHashCode(string filename) { FileStream stream = new FileStream( filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); try { return CalcHashCode(stream); } finally { stream.Close(); } } public static string CalcHashCode(FileStream file) { MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider(); Byte[] hash = md5Provider.ComputeHash(file); return Convert.ToBase64String(hash); } 

EDIT: If you're going to compare a file with others more that one time, you can save the file hash and compare it. For a single comparison, the byte-to-byte comparison is better. You need also to recompute hash when the file changes, but if you're going to do massive comparisons (more than one time), I recommend using the hash approach.

deleted 88 characters in body
Source Link
Daniel Peñalba
  • 32.1k
  • 36
  • 146
  • 226

Well, the quick approach is comparing the write timestamp of the files. If this is enough for you,I'm not sure if you can assume that ifin the files have differentfile write timestamps the files are changed.

  If you cannot rely in the timestampsnot, your unique alternative, is comparing the hashcodecontent of the files. A fast way to do that is calculating the hashcode of the file contents. The following code snippet get the hash for a fileshows how to do it.

 public static string CalcHashCode(string filename) { FileStream stream = new FileStream( filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); try { return CalcHashCode(stream); } finally { stream.Close(); } } public static string CalcHashCode(FileStream file) { MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider(); Byte[] hash = md5Provider.ComputeHash(file); return Convert.ToBase64String(hash); } 

Well, the quick approach is comparing the write timestamp of the files. If this is enough for you, you can assume that if the files have different timestamps the files are changed.

  If you cannot rely in the timestamps, your unique alternative, is comparing the hashcode of the files. The following code snippet get the hash for a file.

 public static string CalcHashCode(string filename) { FileStream stream = new FileStream( filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); try { return CalcHashCode(stream); } finally { stream.Close(); } } public static string CalcHashCode(FileStream file) { MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider(); Byte[] hash = md5Provider.ComputeHash(file); return Convert.ToBase64String(hash); } 

Well, I'm not sure if you can in the file write timestamps. If not, your unique alternative, is comparing the content of the files. A fast way to do that is calculating the hashcode of the file contents. The following code snippet shows how to do it.

 public static string CalcHashCode(string filename) { FileStream stream = new FileStream( filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); try { return CalcHashCode(stream); } finally { stream.Close(); } } public static string CalcHashCode(FileStream file) { MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider(); Byte[] hash = md5Provider.ComputeHash(file); return Convert.ToBase64String(hash); } 
Source Link
Daniel Peñalba
  • 32.1k
  • 36
  • 146
  • 226
Loading