views:

1042

answers:

8

I have following requirement,

  • I have C#/.Net console application, which refers to 'System.Data.Sqlite.dll'
  • 'System.Data.Sqlite.dll' is not registerd in GAC (i don't want to)
  • I don't want to keep it in the directory where the Executable(.exe) is kept.

  • How can i keep the 'System.Data.Sqlite.dll' file at some other directory , and load it safely at runtime, before its getting referenced in application code ?

+4  A: 

I'm not sure why you want to do this again, it's a deviation from the best practices, But to answer your question: You could use

Assembly.LoadFrom Method (String path)

From the System.Reflection Namespace

& having the path in the app.config file

Assembly.LoadFrom on MSDN

AB Kolan
I can't add the path to app.config file, The Dll directory may change at runtime. Any workaround ?
Palani
Why would the dll directory change at runtime?
Martin Harris
@Palani - Could you pls explain your use case briefly
AB Kolan
A: 

You could include the DLL in your EXE project as an embedded resource, and when your application starts you could save the DLL out to your EXE file directory (and delete it when the application ends, although I don't know what the problem is with having the DLL in the EXE directory). This will work as long as you perform the extraction before loading anything that references SqlLite.

Update: since you want to do plugin-style DLLs, you can keep the DLLs in a separate directory, and copy them into the EXE folder on application startup.

MusiGenesis
+1  A: 

You could use Assembly.LoadFrom(filename)

But as commenters pointed out, I don't see why you would want to do that?

Philippe Leybaert
A: 

I can't see the reason for having referenced assemblies apart from the exe file, but one way to do it is to place them in a separate directory under the directory in which the exe file is located and include a probing element in the app.config file.

Fredrik Mörk
A: 

You can use reflection: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.load%28VS.71%29.aspx

Be careful because of performance. Reflection must be used carefully.

Ricardo
+5  A: 

you can use manual assembly resolution to do this.

You need to provide a delegate to the AssemblyResolve event in the current AppDomain

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += assemblyResolver.ResolveEventHandler;

when the application has any assembly references that it can't resolve it will call this delegate to get the assembly resolved. You can then simply return the assembly requested from the delegate:

Assembly assembly = Assembly.LoadFrom (assemblyPath);
return assembly;

hope this helps

Sam Holder
A: 

You will need to load your dll and methods using reflection (using System.Reflection namespace) using a combination of Assembly.LoadFrom and Type.GetType and Type.GetMethod.

Here is an example on how to use reflection to dynamically call a method from a dll loaded at runtime.

static string DynamicMethodCall(string dllPath, string someClass, string someMethodName, object[] parameters)
        {
            Assembly dll = Assembly.LoadFile(dllPath);

            Type type = dll.GetType(someClass);

            if (type == null)
            {
                Exception ex = new Exception("class not found");
                throw ex;
            }

            MethodInfo method = type.GetMethod(someMethodName);

            if (method == null)
            {
                Exception ex = new Exception("method not found");
                throw ex;
            }

            if (parameters.Length >= 1)
            {
                object[] myparam = new object[1];
                myparam[0] = parameters;
                return (string)method.Invoke(null, myparam);
            }
            else
            {
                return (string)method.Invoke(null, null);
            }
        }
William Edmondson
+3  A: 

You can add a probing element to your application's config file. This allows other folders than the default to be checked for required libraries. See here.

It still must be below the exe folder though for security reasons.

Martin Harris