One small function of a large program examines assemblies in a folder and replaces out-of-date assemblies with the latest versions. To accomplish this, it needs to read the version numbers of the existing assembly files without actually loading those assemblies into the executing process.
+3
A:
Use AssemblyName.GetAssemblyName("assembly.dll");
, then parse the name. According to MSDN:
This will only work if the file contains an assembly manifest. This method causes the file to be opened and closed, but the assembly is not added to this domain.
jop
2008-10-09 16:21:33
I'm trying not to think of all the hacks I've implemented trying to get a version number without loading the file. Yeesh.
MusiGenesis
2008-10-09 16:54:44
+6
A:
I found the following in this article.
using System.Reflection;
using System.IO;
...
// Get current and updated assemblies
AssemblyName currentAssemblyName = AssemblyName.GetAssemblyName(currentAssemblyPath);
AssemblyName updatedAssemblyName = AssemblyName.GetAssemblyName(updatedAssemblyPath);
// Compare both versions
if (updatedAssemblyName.Version.CompareTo(currentAssemblyName.Version) <= 0)
{
// There's nothing to update
return;
}
// Update older version
File.Copy(updatedAssemblyPath, currentAssemblyPath, true);
Joel B Fant
2008-10-09 16:21:53
Crap. As always for me, this doesn't work on the Compact Framework. AssemblyName is there, but AssemblyName.GetAssemblyName isn't.
MusiGenesis
2008-10-09 17:10:21
An older trick I used to use (for scanning through plugin assemblies) was to create a sandbox AppDomain, load them in that, and then close down the AppDomain when I was done. Not sure about CF, though.
Joel B Fant
2008-10-09 17:40:56
Won't fit here. But it relies on an object created in the new AppDoain loading the assebly, which means using AppDomain.CreateInstance(), which doesn't seem to be supported in CF. You should ask a new question, link to this one, and specify Compact Framework.
Joel B Fant
2008-10-09 20:39:14
+3
A:
Depending on the files, one option might be FileVersionInfo
- i.e.
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(path)
string ver = fvi.FileVersion;
The problem is that this depends on the code having the [AssemblyFileVersion]
attribute, and it matching the [AssemblyVersion]
attribute.
I think I'd look at the AssemblyName options suggested by others first, though.
Marc Gravell
2008-10-09 16:23:37
Comparing the FileVersionInfo like this could be useful as well. An update to a library which fixes bugs but does not change the API could have the same Assembly version but should have a different file version. So in many cases the AssemblyFileVersion may be the one you want to check.
Rob Parker
2009-11-11 17:48:35