tags:

views:

448

answers:

3

I need to specify path to dlls referenced by assembly in .config file. Problem is that path can be found in env. variable. Is it possible to use some sort of %DLLPATH% macro in .config file?

+2  A: 

Is this a configuration entry that you're reading, or is .NET reading it? If you're reading it yourself, you can do the appropriate substitution yourself (using Environment.ExpandEnvironmentVariables to do the lot or Environment.GetEnvironmentVariable if you want to be more selective).

If it's one that .NET will be reading, I don't know of any way to make it expand environment variables. Is the config file under your control? Could you just rewrite it?

In fact, even if you can do the substitution, is that really what you want to do? If you need to specify the full path to a DLL, I suspect you'll need to find it via the DLLPATH (checking for its presence in each part of the path) and then subtitute %DLLPATH%\Foo.dll with the full path to Foo.dll.

Jon Skeet
+1  A: 

Please go through the articles

http://learn.iis.net/page.aspx/241/configuration-extensibility/

and

http://www.codeproject.com/KB/install/assemblydeployment.aspx?display=Print

then, you can implement your idea.

lakshmanaraj
+4  A: 

Yes, that's possible! Suppose you have something like that in your config:

<configuration>
  <appSettings>
    <add key="mypath" value="%DLLPATH%\foo\bar"/>
  </appSettings>
</configuration>

Then you can easily get the path with:

var pathFromConfig = ConfigurationManager.AppSettings["mypath"];
var expandedPath = Environment.ExpandEnvironmentVariables(pathFromConfig);

ExpandEnvironmentVariables(string s) does the magic by replacing all environment variables within a string with their current values.

Prensen