jtoddcs jtoddcs - 4 months ago 58
C# Question

Finding a unique File ID, based on path, in C#

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);


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.