Testing for references availability programmatically?

  • Thread starter Thread starter Phoenix
  • Start date Start date
P

Phoenix

Hi,

I'd like to do the following:

My main application is a .exe, that uses a .dll included as a
reference in the project.

I'd like to know if it's possible for the .exe to physically test the
presence of the .dll in its directory (i.e. theFile.exists).
So far, instructions to that extent have failed, because the Framework
throws an exception before any code is executed.

So, can I include such a check in the .exe, or do I have to catch the
exception first thing? I don't think that would work, since as I've
said the Framework crashes before, apparently, any code is executed in
the .exe

Thanks for any answer.
 
I'd like to know if it's possible for the .exe to physically test the
presence of the .dll in its directory (i.e. theFile.exists).
So far, instructions to that extent have failed, because the Framework
throws an exception before any code is executed.

So, can I include such a check in the .exe, or do I have to catch the
exception first thing? I don't think that would work, since as I've
said the Framework crashes before, apparently, any code is executed in
the .exe

Yes it's possible - take a look at the System.Reflection namespace.

The reflection namespace provides function to dynamically load DLLs from
disk. Once you have the DLL loaded, you can scan through the assembly to
see if the proper classes are in place, etc.
 
Also note: you don't need to hard code the assemblies: look at:
Assembly.GetExecutingAssembly().GetReferencedAssemblies()
This gives you the "what I reference" as AssemblyName instances,
suitable for passing to Assembly.Load()... so you can call that and see
what you get back. Each only loads once, so this is safe to call. You
may wish to resursively check each as you load it, but watch out that
the 2 main MS libs are circularly referenced (each references the
other), so watch you don't get into an infinite loop... for instance,
keep a list of checked assemblies, and exclude references you have
already checked.

Also - this checking code *must* preceed the first attempt to use them.
Additionally, you cannot use types from the referenced assemblies in
the same method, as JIT works method-by-method.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top