Build Private Bin Path - CSharp System.Reflection

CSharp examples for System.Reflection:Assembly

Description

Build Private Bin Path

Demo Code

// This program is free software; you can redistribute it and/or
using System.Diagnostics;
using System.Reflection;
using System.IO;//  w w  w.  j  a v a 2s.com
using System.Collections;
using System;

public class Main{
        private static void BuildPrivateBinPath (Assembly assembly, string referencedAssembly) {
            DirectoryInfo baseDir = 
                new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
            ArrayList possibleDlls = new ArrayList();
            FileInfo rootMatch = new FileInfo(Path.Combine(baseDir.FullName, referencedAssembly + ".dll"));
            if (rootMatch.Exists) {
                possibleDlls.Add(
                    rootMatch);
            }

            // add some other cantidates
            DirectorySearch(possibleDlls, baseDir, referencedAssembly);
            FileInfo match = null;

            foreach (FileInfo possibleDll in possibleDlls) {
                if (IsMatch(Path.GetDirectoryName(assembly.Location), 
                    Path.GetDirectoryName(possibleDll.FullName))) {
                    match = possibleDll;
                    break;
                }
            }
            if (match == null) {
                throw new Exception("Unable to find correct version of referenced assembly.");
            }
            AppDomain.CurrentDomain.AppendPrivatePath(Path.GetDirectoryName(match.FullName));
        }
}

Related Tutorials