2

I need a way to determine if a user has copied a file to a new location.

Example: You have two computers and you copy file.txt from C:\Temp\ on computer1 to C:\Temp\ on computer2.

Is there an ID associated with these two files, based on their location, that will help me determine if this file has moved?

Update: After some discussion, here is the resulting code. This determines if a file has been copied by creating a Guid using the file path and creation time. This resulting Guid can be compared to a stored Guid to determine if the file has been copied.

FileInfo fi = new FileInfo("C:\\Temp\\temp.txt"); 
string filePathCreationComposite = String.Format("{0}{1}", Path.GetFullPath(fi.FullName), fi.CreationTime); 

using (MD5 md5 = MD5.Create()) 
{ 
   byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(filePathCreationCo‌​mposite)); 
   Guid result = new Guid(hash); 
}
jtoddcs
  • 60
  • 9
  • 1
    I think, you can get a unique `Hash code` of your file path to compare them later. – Paviel Kraskoŭski Oct 28 '16 at 15:43
  • why not just compare the paths for equality? – KMoussa Oct 28 '16 at 15:56
  • Because C:\Temp\File1.txt is equivalent to C:\Temp\File1.txt. This file has been copied from computer1 to the same directory and name as computer2. I need to know that this file has been copied and is effectively in a new location. @Pavieł Kraskoŭski I think your solution will work for what I need. Do you know how to get that unique hash code for the file path? – jtoddcs Oct 28 '16 at 16:00
  • The only way to do what you say I can think of is if you are handling those copies via your application. If not, I don't think the OS is going to keep track of all the copies over a file at all... – jorgonor Oct 28 '16 at 16:04

2 Answers2

0

If you cannot control the process of the file copy (i.e. know 100% that a user does this under your control, e.g. inside your UI), the only possible way of checking this is compare the contents. As far as I know there are no special trustworthy file ID especially across various machines that even stays the same so that you can compare it.

This is of course not a guarantee that a user copied a file from A to B, since the file with exact same content can be copied from anywhere else, but I think you really need only to verify that the two files in the two different locations are the same.

One easy way to compare the content of a file (and to store it's "id") is to calculate a hash. SHA-256 will fit ideally for that purpose.

This is the example of doing it:

using (FileStream stream = File.OpenRead(file))
{
    SHA256Managed sha = new SHA256Managed();
    byte[] checksum = sha.ComputeHash(stream);
    return BitConverter.ToString(checksum).Replace("-", String.Empty);
}

Though you don't need a string of course. This is not a very efficient way, you can read more about it here and here. In the first question they suggest that for some reason SHA-512 is faster to calculate, don't know if it's true and this is.

P.S. It's worth noting that if there is a simplest change in the original file, the hashes won't match anymore.

P.P.S. You can use any faster hash like MD5 or some other one (MD5 is not cryptographically secure, but this should not matter for your case).

Community
  • 1
  • 1
Ilya Chernomordik
  • 23,987
  • 20
  • 99
  • 170
0

You should be able to check either the creation time or last write time with the System.IO.FileInfo class. That, along with path information should be enough to tell you whether a file has changed or moved.

jjxtra
  • 19,175
  • 12
  • 90
  • 136
  • I really like this idea. I think it will solve my problem to create a composite guid based on the changed date and the file path. `FileInfo fi = new FileInfo("C:\\Temp\\temp.txt"); string filePathCreationComposite = String.Format("{0}{1}", Path.GetFullPath(fi.FullName), fi.CreationTime); using (MD5 md5 = MD5.Create()) { byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(filePathCreationComposite)); Guid result = new Guid(hash); }` – jtoddcs Oct 31 '16 at 22:33
  • @jtoddcs thanks. If it works out would love an up vote and accepted answer ;) – jjxtra Nov 01 '16 at 14:11