c# path to an external library

G

Guest

I am writing an app in c#. It uses an external dll library. The library is
always in windows/system32 directory. The problem is that Windows directory
can be located on C:, D: or different disk drive. How to read value od windir
system environment variable in my app?? This variable stores path to Windows
directory. How to tell my app to use the dll library from this location?
 
J

Jianwei Sun

Chris said:
I am writing an app in c#. It uses an external dll library. The library is
always in windows/system32 directory. The problem is that Windows directory
can be located on C:, D: or different disk drive. How to read value od windir
system environment variable in my app?? This variable stores path to Windows
directory. How to tell my app to use the dll library from this location?

An example from msdn.

// Sample for the Environment.GetFolderPath method
using System;

class Sample
{
public static void Main()
{
Console.WriteLine();
Console.WriteLine("GetFolderPath: {0}",

Environment.GetFolderPath(Environment.SpecialFolder.System));
}
}
/*
This example produces the following results:

GetFolderPath: C:\WINNT\System32
*/
 
G

Guest

How are you loading the library, is it through Dll import, or is it an
assembly loaded through Assembly.LoadFrom?

Ciaran O'Donnell
 
G

Guest

There are 2 ways to do this:

1. You could use the codeBase element in your app.config file to identify
the assembly to load from the system folder. for more info see
http://msdn2.microsoft.com/en-us/library/efs781xb.aspx
You could write the path of the codebase section of the app.config file upon
installation to the client computer.

2. You could use Assembly.LoadFrom to load the assembly. If you use this
method, then you will have to use late binding to invoke the methods that you
want, which is not ideal. I personally prefer early binding whenever possible.

Good luck!
 
G

Guest

I added the library by simply clicking project/add reference

the file is added to the same directory where my app exe file is.

that is why I want to load the dll library from windows/system32 directory
I am very newbie in dll libraries, can you explain how to use early binding
please?
 
G

Guest

After you haved added the dll reference, set the copy local value to false
for that dll.
Then add codebase section to your app.config file to redirect calls for that
dll to the windows folder like so:

<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly"
publicKeyToken="32ab4ba45e0a69a1"
culture="neutral" />
<codeBase version="2.0.0.0"
file:///C:/WINDOWS/system32/lib.dll/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>


Interestingly, I would like to see if the file:// reference works with an
environment variable.
try using file:///%windir%/system32/lib.dll and let me know if that works.
You may have to strongly name your assembly.

FYI: Early binding means that you know about the types that you're dealing
with at compile time. This is exactly what you have been doing all along.

Good luck!
 
G

Guest

thats working

but now i need the app.config file with the app. so when i want to give
someone the app i need to attach the config file as well.

is really required?
i tried to use a combination of the code below:

System.Reflection.Assembly asbl =
System.Reflection.Assembly.LoadFile("path_to_dll");
System.Type[] typ = asbl.GetTypes();
System.Reflection.MethodInfo[] met = typ[0].GetMethods();
met[0].Invoke(????);//i have found out that the name of method i need is in
met[0]

but how to use that?

any other ideas?
 
G

Guest

As far as I know, the default probing behaviour of the CLR runtime can be
overriden by configuration file changes (see
http://msdn2.microsoft.com/en-us/library/yx7xezcf(VS.80).aspx). However, if
you do not want to use a configuration file, then I suggest using late
binding using the Assembly.LoadFrom method. This way you can specify exactly
where your dll is residing.

Personally I like to use early binding whereever possible, simply because it
makes for easier code maintenance. I personally do not see a problem in
deploying the app.exe.config file with your application, if you have a proper
setup project. Then all the end user has to worry about is to know how to
install and uninstall your application.

Note: Please mark this post as answered if you are happy with my solution.
Thanks.

--
Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top