Discovering the assembly names in your project ?

  • Thread starter Thread starter vivekian
  • Start date Start date
V

vivekian

Hi ,

Just wondering is there a simple way to extract the names of all the
assemblies placed in your bin folder ?

thanks in advance..
 
Just wondering is there a simple way to extract the names of all the
assemblies placed in your bin folder ?

Well, you can use

System.IO.DirectoryInfo to list the DLL and EXE files

then

System.Reflection.Assembly.LoadFrom to load the assembly (if any) from each
file
 
If you are dealing with a few files using Assembly.LoadFrom (and ust having
it fail if its not ok is fine).

I had found this code some place a long time ago which verifies the image
within the file as opposed to the filename which may be useful.

static bool IsClrImage( string fileName )
{
FileStream fs = null;
try
{
fs = new FileStream( fileName, FileMode.Open, FileAccess.Read,
FileShare.Read );
byte[] dat = new byte[300];
fs.Read( dat, 0, 128 );
if( (dat[0] != 0x4d) || (dat[1] != 0x5a) ) // "MZ" DOS header
return false;

int lfanew = BitConverter.ToInt32( dat, 0x3c );
fs.Seek( lfanew, SeekOrigin.Begin );
fs.Read( dat, 0, 24 ); // read signature & PE file header
if( (dat[0] != 0x50) || (dat[1] != 0x45) ) // "PE" signature
return false;

fs.Read( dat, 0, 96 + 128 ); // read PE optional header
if( (dat[0] != 0x0b) || (dat[1] != 0x01) ) // magic
return false;

int clihd = BitConverter.ToInt32( dat, 208 ); // get IMAGE_COR20_HEADER
rva-address
return clihd != 0;
}
catch( Exception )
{
return false;
}
finally
{
if( fs != null )
fs.Close();
}
}

Cheers,
Greg Young
MVP-C#
 
You can get a list of all the assemblies that your application references by
using the reflection namespace in the framework. When your application is
executing you can call the following code:

using System.Reflection;

AssemblyName[] asms = GetExecutingAssembly().GetReferencedAssemblies();

You can loop through this array and use either the fullname or the name
property.
Not all your referenced assemblies will reside in the bin directory. However,
you could use the CodeBase property to determine the location and parse it
out. This eliminates the need to use any file access logic in your code.
 
Back
Top