0

i want to load all *.dll files from a directory, and to compare the date of each *.dll file with Datetime.Now

i use for this the Assembly factory :

 Assembly assembly = Assembly.LoadFile(path);

            try
            {  
                var myTypes = assembly.GetTypes();

                foreach (var myType in myTypes)
                {.....}
            }

the problem is when i want to replace the current *.dll file with the new *.dll file, i get the error : the *.dll file is used by an other process (My assembly)

so i want to run my assembler on an other domain, and when i cancel this domain my *.dll file will be free.

thx for your help.

Jester
  • 56,577
  • 4
  • 81
  • 125
Kernel Med
  • 321
  • 2
  • 5
  • 11
  • Do you want to detect changes at runtime? –  Dec 03 '14 at 11:08
  • sorry i don't understand, but i want to fetch all Dll files in my directory and i show the types of each Dll files in a ListView, and with a click i want to replace the current dll file with a new dll file, but the probleme is that the dll file is used by a Assembly, so i have to kill the assembly before i have a hand on my file – Kernel Med Dec 03 '14 at 11:16
  • @KernelMed ofcourse its used, you are using it by loading it. – alexo Dec 03 '14 at 11:19
  • so what is the solution to decharge it ? – Kernel Med Dec 03 '14 at 11:21
  • You can't replace dll from program whitch use this dll. – Vlad Dec 03 '14 at 11:23
  • Yes. What's more, you cannot unload assembly from domain that is once loaded. But you can create domain, load these assemblies into newly created one and then unload whole domain after all operations are done. –  Dec 03 '14 at 11:33
  • yes this is what i want to do, but i dont know how ? how to create new domaine, how to run a assembly in this new domaine ?!! – Kernel Med Dec 03 '14 at 11:36
  • Please, reffer: http://stackoverflow.com/a/6259172/2160375 When you will stuck with sth, please ask new question. –  Dec 03 '14 at 11:38

1 Answers1

0

You need do your check at AssemblyResolve event. example:

static void Main()
{
    AppDomain.CurrentDomain.AssemblyResolve += FindAssembly;
    ...
}

and the event:

static Assembly FindAssembly (object sender, ResolveEventArgs args)
{
    //here load assembly and decide which version you want
    Assembly assembly = Assembly.LoadFile(path);
    return assembly;
}
dovid
  • 6,354
  • 3
  • 33
  • 73