none
Load dll from different directory RRS feed

  • Question

  • Hi,

    my referenced dlls are in lib directory. I can load them at form load event with following I have found on net:

     Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            {
                //This handler is called only when the common language runtime tries to bind to the assembly and fails.
    	
                //Retrieve the list of referenced assemblies in an array of AssemblyName.
                Assembly MyAssembly, objExecutingAssemblies;
                string strTempAssmbPath = "";
    	
                objExecutingAssemblies = Assembly.GetExecutingAssembly();
                AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();
    	
                
                //Loop through the array of referenced assembly names.
                foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
                {
                    //MessageBox.Show(strAssmbName.Name);
                    //Check for the assembly names that have raised the "AssemblyResolve" event.
                    if (strAssmbName.Name == "BSE.Windows.Forms" || strAssmbName.Name == "GMap.NET.Core" || strAssmbName.Name == "GMap.NET.WindowsForms")
                    {
                        //Build the path of the assembly from where it has to be loaded.
                        //The following line is probably the only line of code in this method you may need to modify:
                        strTempAssmbPath = "lib/";
                        if (strTempAssmbPath.EndsWith("\\")) strTempAssmbPath += "\\";
                        strTempAssmbPath += args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
                        break;
                    }
    	
                }
                //Load the assembly from the specified path.
                MyAssembly = Assembly.LoadFrom(strTempAssmbPath);
    	
                //Return the loaded assembly.
                return MyAssembly;
    
                
            }
    
            private void OknoProgramu_Load(object sender, EventArgs e)
            {
                //The AssemblyResolve event is called when the common language runtime tries to bind to the assembly and fails.
                AppDomain currentDomain = AppDomain.CurrentDomain;
                
                currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);
    
            }
    Libs are loaded and controls are working, until they try to load referenced assemblies by themselves.(For example system.data.sqlite). It thorws then filenotfound exception. How can I reference them properly?
    • Edited by Muffon Friday, December 2, 2011 12:46 PM
    • Moved by Helen Zhou Monday, December 5, 2011 7:21 AM (From:Windows Forms General)
    Friday, December 2, 2011 12:41 PM

Answers

  • you don't need manually load sqlite dlls, latest GMap.NET version works on any target without doing anything, you problem was solved for you and for many others, check the latest release
    • Marked as answer by Paul Zhou Monday, December 12, 2011 8:45 AM
    Wednesday, December 7, 2011 10:06 PM
  • If the loaded assembly has some dependency and dependencies are not in GAC, you need to locate the dependencies in the same direction of the assembly.

     


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Paul Zhou Monday, December 12, 2011 8:45 AM
    Friday, December 9, 2011 8:56 AM

All replies

  • I would have expected that anything called System.Data.* would be in the Global Assembly Cache which should be loadable from anywhere.

    Are you sure that this assembly is actually installed?

     


    Regards, Phill W.
    Friday, December 2, 2011 1:13 PM
  • Of course, it is.
    Saturday, December 3, 2011 4:35 PM
  • Hello Muffon,

    This question is more related to  Common Language Runtime , I'm moving it to that forum to help it get better support.

    Sincerely,

    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 5, 2011 7:21 AM
  • Hi,

    It seems that the code "MyAssembly = Assembly.LoadFrom(strTempAssmbPath);" threw a FileNotFound exception, am I right?

    Have you set breakpoint there and debug the application? Then see whether the "strTempAssmbPath" has got a correct value. Is it a valid folder? Is it where the dll is located?

    Could you please try to load it directly? Is it successfully to load it using the full path instead? Moreover, could you please try to load other dlls instead to test?

    Look forward to hearing from you.

    Have a nice day.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Paul Zhou Monday, December 5, 2011 7:52 AM
    Monday, December 5, 2011 7:51 AM
  • Hello, thanks for reply.

    I don't get FileNotFoundException when loading defined DLLs. It is loaded from defined destination and application will get it.

    The problem is, that I am getting this exception when the loaded component requires for ex. System.Data.SqlLite itself and it fails to load it, probably because I have defined custom assembly.

    Is it clear?

    Maybe I don't understand loading reference from custom location and I am using the code in bad way.

     


    • Edited by Muffon Monday, December 5, 2011 2:19 PM
    Monday, December 5, 2011 2:12 PM
  • You said " when the loaded component requires for ex. System.Data.SqlLite itself ". Do you mean you want to itself in an assembly? For example, In an assembly named xxx.dll, you want to load itself(xxx.dll) again?

    If you want to do that, may I ask why you try to do it? 


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, December 6, 2011 9:25 AM
  • you don't need manually load sqlite dlls, latest GMap.NET version works on any target without doing anything, you problem was solved for you and for many others, check the latest release
    • Marked as answer by Paul Zhou Monday, December 12, 2011 8:45 AM
    Wednesday, December 7, 2011 10:06 PM
  • If the loaded assembly has some dependency and dependencies are not in GAC, you need to locate the dependencies in the same direction of the assembly.

     


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Paul Zhou Monday, December 12, 2011 8:45 AM
    Friday, December 9, 2011 8:56 AM
  • Hi,

     

    Has your issue been resolved? Would you mind letting us know the result of the suggestions?

     

    Now I will mark an answer, you can mark others that you think to be so useful to your issue.

    If you still have any questions about this issue, please feel free to let me know. 


    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 12, 2011 8:45 AM
  • Hello, I haven't tried new version. But the reason I was doing this defining of  custom assembly was the wrong design of .Net.

    I just wanted to have referenced .dlls in another directory(for example lib).  I gived up to do this because it looks that I can do more

    mess than goodness.


    • Edited by Muffon Wednesday, February 8, 2012 12:27 PM
    Wednesday, February 8, 2012 12:27 PM