views:

208

answers:

6

I want to log the versions of all DLLs my .NET-application uses. It doesn't matter if the log-output is generated on startup or on first use of each DLL.

The first solution which came to my mind was to iterate over all DLL files which reside in the same directory as my assembly. But is this the best option I have? Is there any better way to do this? It's important that the solution should also work on .NET-Compact-Framework.

A: 

I am not sure if it is available on CF, but in normal .NET, I would simply use AppDomain.GetAssemblies().

You can use the AppDomain.AssemblyLoad event to notify you when an assembly is loaded.

leppie
Unfortunately both the method and the event are not available on CF.
Bob
A: 

You might be able to use the Dependency Walker, and just use the command-line options to execute it as a subprocess. According to the FAQ, it works against Windows CE modules, though it doesn't run on CE devices, so that may not suit your needs.

Jason R. Coombs
A: 

Use the AppDomain.AssemblyLoad event.

e.g.

AppDomain.CurrentDomain.AssemblyLoad+=(s, e) => 
  Console.WriteLine("loaded: "+e.LoadedAssembly.FullName);
chris
Unfortunately the AssemblyLoad-Event is not available on Compact Framework.
Bob
A: 
public void LogAssemblies()
{
  foreach(Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
   { 
     AssemblyName assemblyName = asm.GetName();
     string simpleName = assemblyName.Name;
     string version = assemblyName.Version.ToString();
     // do something with these
  }
}

EDIT: Woops, looks like AppDomain.GetAssemblies() is not in compact framework.

ray2k
+6  A: 

Edit: I tested and verified that this works on the .NET Compact Framework.

You could do this using Mono.Cecil, a powerful tool that allows you to open and inspect .NET assemblies at the IL level without even loading them into an AppDomain.

string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;

// If using Mono.Cecil 0.6.9.0:
AssemblyDefinition myAssembly = AssemblyFactory.GetAssembly(path);

// If using Mono.Cecil 0.9.1.0:
AssemblyDefinition myAssembly = AssemblyDefinition.ReadAssembly(path);

// from there, you can inspect the assembly's references
foreach (ModuleDefinition module in myAssembly.Modules)
{
    foreach (AssemblyNameReference assemblyReference in module.AssemblyReferences)
    {
        // do something with the reference e.g get name, version, etc
        string fullName = assemblyReference.FullName;
        Version version = assemblyReference.Version;
    }
}
Zach Johnson
How is that even remotely helpful for the COmpact Framework?
ctacke
@ctacke: Mono.Cecil is not a Mono specific library; you deploy it with your application. I have verified that it indeed works on the CF.
Zach Johnson
@Zach: then that is just flipping cool!
ctacke
A: 

I guess an interesting algorithm would be to serialize the assemblies you're using, then, upon startup, you deserialize your configuration and compare with what version you have of each DLL. Then, upon version update, you serialize a new version of your configuration and log your assembly name and version to your logfile or so.

Keep in mind that the most important thing to consider is get the thing does what you need it to do. Nothing else matters. Once it does the job you require, there's perhaps room for improvement. But first, get it working! You'll simply see what needs to be improved afterwards.

Will Marcouiller