Download AssemblyReferenceScanner.zip, last updated 06/10/2012 (90.98 KB)

Download
  • md5: 47bd4bf571be76c33b954d358dca933b
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Cyotek.Tools.AssemblyReferenceScanner
{
  internal class AssemblyScanner
  {
    #region��Private�Member�Declarations

    private AssemblyInfoCollection _results;

    #endregion��Private�Member�Declarations

    #region��Public�Constructors

    public AssemblyScanner()
    {
      this.Exclusions = new List<string>();
    }

    #endregion��Public�Constructors

    #region��Public�Methods

    public AssemblyInfoCollection Scan(string folder)
    {
      if (string.IsNullOrEmpty(folder))
        throw new ArgumentNullException("folder");
      else if (!Directory.Exists(folder))
        throw new DirectoryNotFoundException();

      _results = new AssemblyInfoCollection();

      this.ScanFiles(folder, "*.dll");
      this.ScanFiles(folder, "*.exe");

      return _results;
    }

    #endregion��Public�Methods

    #region��Public�Properties

    public List<string> Exclusions { get; set; }

    #endregion��Public�Properties

    #region��Private�Methods

    private void ScanFile(string fileName)
    {
      Assembly assembly;

      try
      {
        assembly = Assembly.ReflectionOnlyLoadFrom(fileName);
      }
      catch (BadImageFormatException)
      {
        Debug.WriteLine(fileName);
        assembly = null;
      }

      if (assembly != null)
      {
        AssemblyInfo info;

        info = new AssemblyInfo()
        {
          FileName = fileName,
          Name = assembly.GetName().Name,
          Version = assembly.GetName().Version
        };

        foreach (AssemblyName dependency in assembly.GetReferencedAssemblies())
        {
          if (!this.ShouldSkip(dependency.Name))
          {
            info.Dependencies.Add(new AssemblyNameInfo()
            {
              Name = dependency.Name,
              Version = dependency.Version
            });
          }
        }

        _results.Add(info);
      }
    }

    private void ScanFiles(string folder, string mask)
    {
      foreach (string fileName in Directory.GetFiles(folder, mask, SearchOption.AllDirectories))
        this.ScanFile(fileName);
    }

    private bool ShouldSkip(string name)
    {
      bool result;

      result = false;
      foreach (string exclusion in this.Exclusions)
      {
        if (Regex.IsMatch(name, exclusion))
        {
          result = true;
          break;
        }
      }

      return result;
    }

    #endregion��Private�Methods
  }
}

Donate

Donate