Reputation: 1058
I have implemented the fusion.dll wrapper mentioned in many posts and now find that at least one dll I need to determine if it needs to be updated is not using build and revision numbers. Consequently I cannot compare on version numbers and need to compare on Last Modified date.
fusion.dll or it's wrappers have no such method which I guess is fair enough but how do I determine the 'real' path of the dll so that I can discovers it's last Modified date.
My code so far:
private DateTime getGACVersionLastModified(string DLLName)
{
FileInfo fi = new FileInfo(DLLName);
string dllName = fi.Name.Replace(fi.Extension, "");
DateTime versionDT = new DateTime(1960,01,01);
IAssemblyEnum ae = AssemblyCache.CreateGACEnum();
IAssemblyName an;
AssemblyName name;
while (AssemblyCache.GetNextAssembly(ae, out an) == 0)
{
try
{
name = GetAssemblyName(an);
if (string.Compare(name.Name, dllName, true) == 0)
{
FileInfo dllfi = new FileInfo(string.Format("{0}.dll", name.Name));
if (DateTime.Compare(dllfi.LastWriteTime, versionDT) >= 0)
versionDT = dllfi.LastWriteTime;
}
}
catch (Exception ex)
{
logger.FatalException("Unable to get version number: ", ex);
}
}
return versionDT;
}
Upvotes: 1
Views: 2826
Reputation: 8814
From the problem description in your question I can see there are really 2 primary tasks that you are trying to accomplish:
1) Determine if a given assembly name can be loaded from the GAC.
2) Return the file modified date for the given assembly.
I believe these 2 points can be accomplished in a much simpler fashion and without having to work with the unmanaged fusion API. An easier way to go about this task might be as follows:
static void Main(string[] args)
{
// Run the method with a few test values
GetAssemblyDetail("System.Data"); // This should be in the GAC
GetAssemblyDetail("YourAssemblyName"); // This might be in the GAC
GetAssemblyDetail("ImaginaryAssembly"); // This just plain doesn't exist
}
private static DateTime? GetAssemblyDetail(string assemblyName)
{
Assembly a;
a = Assembly.LoadWithPartialName(assemblyName);
if (a != null)
{
Console.WriteLine("'{0}' is in GAC? {1}", assemblyName, a.GlobalAssemblyCache);
FileInfo fi = new FileInfo(a.Location);
Console.WriteLine("'{0}' Modified: {1}", assemblyName, fi.LastWriteTime);
return fi.LastWriteTime;
}
else
{
Console.WriteLine("Assembly '{0}' not found", assemblyName);
return null;
}
}
An example of the resulting output:
'System.Data' is in GAC? True
'System.Data' Modified: 10/1/2010 9:32:27 AM
'YourAssemblyName' is in GAC? False
'YourAssemblyName' Modified: 12/30/2010 4:25:08 AM
Assembly 'ImaginaryAssembly' not found
Upvotes: 1