views:

342

answers:

2

Hi:

I'm trying to create a diagnostic log for my application that will display the latest version number of an assembly installed in the GAC. For example, there are two versions of the same assembly in the GAC: foo.dll version 1.0.0.0 and foo.dll version 2.0.0.0. I need a function like the following:

GetLatestGacVersion("foo.dll");  // returns "2.0.0.0"

Anyone know the best way to do this?

Thanks!

+2  A: 

Using a managed wrapper around the Fusion API (fusion.dll) you could enumerate the assemblies in the GAC, filter them by name and order by version.

Darin Dimitrov
Admittedly, this is probably the more correct way to accomplish my task. However, the deprecated LoadWithPartialName method seems to meet my minimal requirements for most cases with just one line of code and no COM interop. Thank you very much for the advice!
Ken
A: 

Easiest is:

Assembly a = Assembly.LoadWithPartialName ("foo.dll");
return a.GetName ().Version

which will automatically give you the latest-version assembly from the GAC.

Please note that the Method is deprecated for good reasons. Asking for an unspecific version from the GAC is going to possibly cause lots of trouble.

Without really knowing what you want to do it's hard to give further advice, but in general if you are looking for a specific version you should rather probe for it instead of just loading "something".

Foxfire
To clarify: I'm not trying to do anything more than what the question asks, and that is: Find the latest version number of a GAC assembly so that it can be logged. I'm being cautious, as this a deprecated method. However, I think my application of it is safe because I won't be using the Assembly object returned by the LoadWithPartialName() method for anything else.From MSDN: "This method first calls Load. If the assembly is not found, this method returns the assembly from the global assembly cache that has the same simple name, and the highest version number."Thanks a lot!
Ken