Search code examples
.netcompact-frameworkassembliesversion

Getting Version of an Assembly without loading it in .NET Compact Framework


In a .NET Compact Framework 3.5 application I'm doing a version check in a fairly tight loop in order to know whether or not another assembly needs to be "upgraded" (have a new copy in another directory copied over it, then start it up again).

The problem is that Assembly.LoadFrom(path).GetName().Version locks the file and prevents said copying over.

AssemblyName.GetAssemblyName(path) would be a better way to get this (thanks to this SO answer), as it doesn't permanently load the assembly into the AppDomain and thus doesn't lock the file, but it isn't available in the Compact Framework.

I could create a new AppDomain, but can't use the new domain's Load method as that's not supported in the Compact Framework.

As a last resort I thought I'd allow the assembly to actually be loaded with the Assembly.Load(byte[]) overload, which would cause a massive memory "leak" in a tight loop since they're never unloaded. To counter that I intended to first hash the assembly's byte array and check a cache of versions for a previous hit. However, you guessed it, the byte array overload for Assembly.Load() isn't supported in the Compact Framework.

I also considered adding AssemblyFileVersions, since they're easier to check anyway, but that's another thing in the growing heap of things not supported in the Compact Framework (my thanks to this SO answer for not having to try this).

Please, tell me what I'm trying to do is possible so I stop hating this framework so much?


Solution

  • I think you are going to have to P/Invoke GetFileVersionInfo, GetFileVersionInfoSize, and VerQueryValue to get what your want. The pinvoke.net site is a great tool for signatures and examples on such things. They have GetFileVersionInfo prototyped, but not the others. Let me know if you need help with them and I can probably get the signatures that will work.

    http://pinvoke.net/default.aspx/coredll/GetFileVersionInfo.html

    Also noticed the OpenNETCF library has this implemented in the OpenNETCF.Diagnostics.FileVersionInfo class. So you can download that library and see how they did it.