4

I am trying to figure out when an assembly has changed (even when version number is the same).

Best effort for now is finding out it has been recompiled, therefore would like to use the same item that MSBuild uses, which I believe is the files GUID stored in meta data? (As per The .NET File Format)

Is there any way I can access this info without re-inventing the wheel and dropping down to file bytes?

Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118
  • 1
    Useful https://stackoverflow.com/questions/502303/how-do-i-programmatically-get-the-guid-of-an-application-in-net2-0? – DavidG Aug 10 '17 at 10:36
  • Hmm, no, the [Guid] is useless. The MVID could be useful, the compiler has to generate a new one when it creates the assembly. None of this ever tells you that the assembly actually changed, just that the file was recreated. Something that the file date already tells you. – Hans Passant Aug 10 '17 at 12:38
  • @HansPassant problem with file date is, if you use a dumb copy which doesn't preserve modified date, then you lose that fact, not the biggest problem in the world, but will be the fall back if I can't use the same thing MSBuild uses. – Michal Ciechan Aug 10 '17 at 13:45
  • @DavidG will look that up + play around with it, looks promising at first shoud (at least the wrong answer[System.Reflection.Assembly GUID] of that question!) – Michal Ciechan Aug 10 '17 at 13:50
  • Who the heck *ever* uses a "dumb copy"?? It doesn't matter anyway, you'd conclude "it has changed" which is indistinguishable from it getting rebuilt. This is not a real problem. – Hans Passant Aug 10 '17 at 13:55
  • @HansPassant FTP, SSH? – Michal Ciechan Aug 10 '17 at 13:57
  • @HansPassant but yes, it is so rare that I think LastModifiedTime is looking like the best option – Michal Ciechan Aug 10 '17 at 14:00
  • For Completeness just came across this (Compile Date + Time): https://stackoverflow.com/questions/1276437/compile-date-and-time – Michal Ciechan Aug 10 '17 at 14:00

2 Answers2

1

Well, it seems that assemblies do not have associated GUIDs. Modules, classes, methods, fields have GUIDs, and the "GUID" stream in a assembly is a place to store them.

If you want to access meta infomations of a assembly (e.g. GUID of its main module), Mono.Cecil may be a good option.

But instead, you can achieve that by calucating a checksum using a hash function, or just taking a look at the modification date. System.IO.FilesystemWatcher is able to inform you, if you do not want to access the file intervally.

qnnnnez
  • 66
  • 1
  • 5
0

Try to store in a variable last modified date of the assembly and check against it. Here is how to get last modified date of an assembly:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.FileInfo fileInfo = new System.IO.FileInfo(assembly.Location);
DateTime lastModified = fileInfo.LastWriteTime;
ohadinho
  • 6,894
  • 16
  • 71
  • 124