I am trying to modify 2 .exes to load DevExpress dlls from 1 location.
The .exes in the "Products" folder are use the same .dlls as the launcher does. I want to avoid having to put the same .dlls into the Products directory, and instead have the .exes read from 1 directory back(the launchers directory).
How can I achieve this?
You can handle the AppDomain.AssemblyResolve event and load the assemblies from the directory yourself using Assembly.LoadFile giving the fullpath to the assembly it is trying to resolve.
Example:
.
.
.
// elsewhere at app startup time attach the handler to the AppDomain.AssemblyResolve event
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
.
.
.
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
AssemblyName assemblyName = new AssemblyName(args.Name);
// this.ReadOnlyPaths is a List<string> of paths to search.
foreach (string path in this.ReadOnlyPaths)
{
// If specified assembly is located in the path, use it.
DirectoryInfo directoryInfo = new DirectoryInfo(path);
foreach (FileInfo fileInfo in directoryInfo.GetFiles())
{
string fileNameWithoutExt = fileInfo.Name.Replace(fileInfo.Extension, "");
if (assemblyName.Name.ToUpperInvariant() == fileNameWithoutExt.ToUpperInvariant())
{
return Assembly.Load(AssemblyName.GetAssemblyName(fileInfo.FullName));
}
}
}
return null;
}
You may set folder(s) path in assemblyBinding>probing::privatePath tag in app.config for the common language runtime to search when loading assemblies.
like this code
Reference MSDN
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="libs" />
</assemblyBinding>
</runtime>
</configuration>
You can create a class:
using System;
using System.Reflection;
namespace myNamespace
{
public sealed class EntryPoint
{
[STAThread]
public static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
var app = new App();
app.InitializeComponent();
app.Run();
}
private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
// do whatever necessary...
}
}
}
As WPF already has a built-in Main() method, you will get a compiler error. So go to project properties > Application > "Startup object" and set it to myNamespace.EntryPoint. In your own Main() method, you have full control over everything, so you can set the AssemblyResolve handler before instantiating the App.
I created a CLI application which actually uses the standard config app.Config file.
In this file I put some subsections, like
<typicsTable>
<mainSettings>
<add key="sheetNumber" value="1"/>
<add key="firstDataRow" value="2"/>
</mainSettings>
</typicsTable>
I actually read these settings with
NameValueCollection TypicsConversionTableSettings = (NameValueCollection)ConfigurationManager.GetSection("typicsTable/mainSettings");
int ctSheetNumber = Int32.Parse(TypicsConversionTableSettings["sheetNumber"]);
int ctFirstDataRow = Int32.Parse(TypicsConversionTableSettings["firstDataRow"]);
Everything works fine in this way.
What I want to do now is
1) I want different config files with custom names (i.e. test1.config , test2.config) and take via CLI the right config file;
2) switch to a less ".net config file", and take data from a standard XML file.
I'm now focusing on the point 1, I tried different attempts, I used
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = #"C:\folderTest\conf1.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
But I absolutely don't get how to read sections AND subsections in the file. How can I do that?
The class that is going to help you, I believe, is System.Xml.Linq.
using System.Xml.Linq;
So Part 1 would be load the file into an XElement:
XElement xConfig = XElement.Load("app.simulated.config");
Here's a quick demo of how you can iterate through everything and also find a single element using a matching condition.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Iterating the config file values and attributes...");
Console.WriteLine("==================================================");
XElement xConfig = XElement.Load("app.simulated.config");
foreach (var element in xConfig.DescendantsAndSelf())
{
Console.WriteLine(element.Name);
foreach (var attribute in element.Attributes())
{
Console.WriteLine("\t" + attribute.Name + "," + attribute.Value);
}
}
Console.WriteLine();
Console.WriteLine("Finding a value using matching conditions.");
Console.WriteLine("==========================================");
XElement xel =
xConfig
.DescendantsAndSelf()
.FirstOrDefault(match =>
(match.Attribute("key") != null) &&
(match.Attribute("key").Value == "sheetNumber"));
Console.WriteLine(
"The value of 'sheetNumber' is " +
xel.Attribute("value").Value
);
// Pause
Console.ReadKey();
}
}
Clone or Download this example from GitHub.
I'm trying to build my own library of most often used methods/code. The method on which I'm currently working suppose to read all pairs (value, key) from provided section by user from the config file and log them into the logger. When I converted code into method and move it to separate file I found a problem related to location of App.config.
So I have my App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="General" type="System.Configuration.NameValueSectionHandler"/>
<section name="Generalxxxxxxxxxxxxx" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<General>
<add key="ApplicationName" value="Configuration Example Project"/>
<add key="Language" value="CSharp"/>
<add key="SecretKey" value="012345"/>
</General>
<Generalxxxxxxxxxxxxx>
<add key="ApplicationName" value="Configuration Example Project"/>
<add key="Language" value="CSharp"/>
<add key="SecretKey" value="012345"/>
</Generalxxxxxxxxxxxxx>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
From which I could read all pairs (key, value) by simply using following code. In this case when everything was under one file I didn't have to provide the App.config location.
var general = ConfigurationManager.GetSection("General") as NameValueCollection;
if (applicationSettings.Count == 0)
{
Console.WriteLine("Application Settings are not defined");
}
else
{
foreach (var key in general.AllKeys)
{
Console.WriteLine(key + " = " + applicationSettings[key]);
}
}
I found a way to pass a location but now I can't read sectionLoad as NameValueCollection because of conversion error so I can't use AllKeys method.
public static Tuple<string, string> LogSaveConfig(string location, string sectionName)
{
//var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var config = ConfigurationManager.OpenExeConfiguration(location);
var sectionLoad = config.GetSection(sectionName);
return Tuple.Create("key", "value"); //just temporary
}
Also I did some debugging on "sectionLoad" and only place where I could found the pairs(key,values) was rawXML:
My question is how can I get the pairs(key, value) when I'm calling config file through method which is part of separate library? My goal is to be able to type MyLib.LoggerDefined.LogSaveConfig(location, section);
which is going to log the pairs(key, value) into logger. Also if my approach is wrong please let me know. Thanks.
You could read the xml file directly which is xml format. I used xml linq and created a dictionary
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication158
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Dictionary<string, List<List<KeyValuePair<string, string>>>> dict = doc.Root.Elements()
.GroupBy(x => x.Name.LocalName, y => y.Elements()
.Select(z => z.Attributes().Select(a => new KeyValuePair<string, string>(a.Name.LocalName, (string)a)).ToList()).ToList())
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
To recreate my production environment I created the following folder structure:
c:\TEST\tested.dll
c:\TEST\tested\tools.dll
The tested.dll is compiled using the following App.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="tested"/>
</assemblyBinding>
</runtime>
</configuration>
As far as I know the application should look for it's tools file in the subfolder. When I try to start the station I still get the error that the file was not found.
To give some context here is an example tested.dll source:
namespace ConsoleApplication1
{
public static class Testable
{
public static tools.IToolAble usefultool = null;
public static void initialisation()
{
if (usefultool == null) usefultool = new UsefulTest()
}
}
public class UsefulTest : tools.IToolAble
{
}
}
and an example tools.dll source:
namespace tools
{
public interface IToolAble
{
}
}
The code that crashes is my testcode that works like this:
private CustomMock controller = new CustomMock();
public void TestFixtureSetUp()
{
controller.LoadFrom(#"c:\TEST\tested.dll");
//The next line crashes because tools assembly is needet but not found
controller.InvokeInitialisation();
}
What am I missing?
Is the App.config correct?
EDIT:
The Answer below is correct, the path is only known once the correct dll can be chosen. So the other team has to add a new ResolveEventHandler before loading. Here is a simplified version of that:
internal void AddResolveEventHandler(string assemblyname, string assemblylocation)
{
AppDomain.CurrentDomain.AssemblyResolve +=
new ResolveEventHandler(
(sender, args) =>
{
Assembly ret = null;
if (
new AssemblyName(args.Name).Name == assemblyname &&
File.Exists(assemblylocation))
{
ret = Assembly.LoadFrom(assemblylocation);
}
return ret;
}
);
}
the tested.dll is compiled using the following App.config file
It needs to be an yourapp.exe.config file, not a .config file for the DLL. The CLR only ever looks for a .config file associated with the main process.
And watch out for app.vshost.exe.config, required when you debug with the hosting process enabled.
And watch out when using unit test runners, another .exe file
Do consider if this is actually worth the trouble. Your user won't care where the DLL is located.
Is it is possible to do something like the following in the app.config or web.config files?
<appSettings>
<add key="MyBaseDir" value="C:\MyBase" />
<add key="Dir1" value="[MyBaseDir]\Dir1"/>
<add key="Dir2" value="[MyBaseDir]\Dir2"/>
</appSettings>
I then want to access Dir2 in my code by simply saying:
ConfigurationManager.AppSettings["Dir2"]
This will help me when I install my application in different servers and locations wherein I will only have to change ONE entry in my entire app.config.
(I know I can manage all the concatenation in code, but I prefer it this way).
A slightly more complicated, but far more flexible, alternative is to create a class that represents a configuration section. In your app.config / web.config file, you can have this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- This section must be the first section within the <configuration> node -->
<configSections>
<section name="DirectoryInfo" type="MyProjectNamespace.DirectoryInfoConfigSection, MyProjectAssemblyName" />
</configSections>
<DirectoryInfo>
<Directory MyBaseDir="C:\MyBase" Dir1="Dir1" Dir2="Dir2" />
</DirectoryInfo>
</configuration>
Then, in your .NET code (I'll use C# in my example), you can create two classes like this:
using System;
using System.Configuration;
namespace MyProjectNamespace {
public class DirectoryInfoConfigSection : ConfigurationSection {
[ConfigurationProperty("Directory")]
public DirectoryConfigElement Directory {
get {
return (DirectoryConfigElement)base["Directory"];
}
}
public class DirectoryConfigElement : ConfigurationElement {
[ConfigurationProperty("MyBaseDir")]
public String BaseDirectory {
get {
return (String)base["MyBaseDir"];
}
}
[ConfigurationProperty("Dir1")]
public String Directory1 {
get {
return (String)base["Dir1"];
}
}
[ConfigurationProperty("Dir2")]
public String Directory2 {
get {
return (String)base["Dir2"];
}
}
// You can make custom properties to combine your directory names.
public String Directory1Resolved {
get {
return System.IO.Path.Combine(BaseDirectory, Directory1);
}
}
}
}
Finally, in your program code, you can access your app.config variables, using your new classes, in this manner:
DirectoryInfoConfigSection config =
(DirectoryInfoConfigSection)ConfigurationManager.GetSection("DirectoryInfo");
String dir1Path = config.Directory.Directory1Resolved; // This value will equal "C:\MyBase\Dir1"
You can accomplish using my library Expansive. Also available on nuget here.
It was designed with this as a primary use-case.
Moderate Example (using AppSettings as default source for token expansion)
In app.config:
<configuration>
<appSettings>
<add key="Domain" value="mycompany.com"/>
<add key="ServerName" value="db01.{Domain}"/>
</appSettings>
<connectionStrings>
<add name="Default" connectionString="server={ServerName};uid=uid;pwd=pwd;Initial Catalog=master;" provider="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Use the .Expand() extension method on the string to be expanded:
var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
connectionString.Expand() // returns "server=db01.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"
or
Use the Dynamic ConfigurationManager wrapper "Config" as follows (Explicit call to Expand() not necessary):
var serverName = Config.AppSettings.ServerName;
// returns "db01.mycompany.com"
var connectionString = Config.ConnectionStrings.Default;
// returns "server=db01.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"
Advanced Example 1 (using AppSettings as default source for token expansion)
In app.config:
<configuration>
<appSettings>
<add key="Environment" value="dev"/>
<add key="Domain" value="mycompany.com"/>
<add key="UserId" value="uid"/>
<add key="Password" value="pwd"/>
<add key="ServerName" value="db01-{Environment}.{Domain}"/>
<add key="ReportPath" value="\\{ServerName}\SomeFileShare"/>
</appSettings>
<connectionStrings>
<add name="Default" connectionString="server={ServerName};uid={UserId};pwd={Password};Initial Catalog=master;" provider="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Use the .Expand() extension method on the string to be expanded:
var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
connectionString.Expand() // returns "server=db01-dev.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"
Good question.
I don't think there is. I believe it would have been quite well known if there was an easy way, and I see that Microsoft is creating a mechanism in Visual Studio 2010 for deploying different configuration files for deployment and test.
With that said, however; I have found that you in the ConnectionStrings section have a kind of placeholder called "|DataDirectory|". Maybe you could have a look at what's at work there...
Here's a piece from machine.config showing it:
<connectionStrings>
<add
name="LocalSqlServer"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
Usally, I end up writing a static class with properties to access each of the settings of my web.config.
public static class ConfigManager
{
public static string MyBaseDir
{
return ConfigurationManager.AppSettings["MyBaseDir"].toString();
}
public static string Dir1
{
return MyBaseDir + ConfigurationManager.AppSettings["Dir1"].toString();
}
}
Usually, I also do type conversions when required in this class. It allows to have a typed access to your config, and if settings change, you can edit them in only one place.
Usually, replacing settings with this class is relatively easy and provides a much greater maintainability.
I thought I just saw this question.
In short, no, there's no variable interpolation within an application configuration.
You have two options
You could roll your own to substitute variables at runtime
At build time, massage the application configuration to the particular specifics of the target deployment environment. Some details on this at dealing with the configuration-nightmare
You have a couple of options. You could do this with a build / deploy step which would process your configuration file replacing your variables with the correct value.
Another option would be to define your own Configuration section which supported this. For example imagine this xml:
<variableAppSettings>
<variables>
<add key="#BaseDir" value="c:\Programs\Widget"/>
</variables>
<appSettings>
<add key="PathToDir" value="#BaseDir\Dir1"/>
</appSettings>
</variableAppSettings>
Now you would implement this using custom configuration objects which would handle replacing the variables for you at runtime.
You can use environment variables in your app.config for that scenario you describe
<configuration>
<appSettings>
<add key="Dir1" value="%MyBaseDir%\Dir1"/>
</appSettings>
</configuration>
Then you can easily get the path with:
var pathFromConfig = ConfigurationManager.AppSettings["Dir1"];
var expandedPath = Environment.ExpandEnvironmentVariables(pathFromConfig);
Inside <appSettings> you can create application keys,
<add key="KeyName" value="Keyvalue"/>
Later on you can access these values using:
ConfigurationManager.AppSettings["Keyname"]
I would suggest you DslConfig. With DslConfig you can use hierarchical config files from Global Config, Config per server host to config per application on each server host (see the AppSpike).
If this is to complicated for you you can just use the global config Variables.var
Just configure in Varibales.var
baseDir = "C:\MyBase"
Var["MyBaseDir"] = baseDir
Var["Dir1"] = baseDir + "\Dir1"
Var["Dir2"] = baseDir + "\Dir2"
And get the config values with
Configuration config = new DslConfig.BooDslConfiguration()
config.GetVariable<string>("MyBaseDir")
config.GetVariable<string>("Dir1")
config.GetVariable<string>("Dir2")
I don't think you can declare and use variables to define appSettings keys within a configuration file. I've always managed concatenations in code like you.
I'm struggling a bit with what you want, but you can add an override file to the app settings then have that override file set on a per environment basis.
<appSettings file="..\OverrideSettings.config">
For rolling out products where we need to configure a lot of items with similar values, we use small console apps that read the XML and update based on the parameters passed in. These are then called by the installer after it has asked the user for the required information.
I would recommend following Matt Hamsmith's solution. If it's an issue to implement, then why not create an extension method that implements this in the background on the AppSettings class?
Something like:
public static string GetValue(this NameValueCollection settings, string key)
{
}
Inside the method you search through the DictionaryInfoConfigSection using Linq and return the value with the matching key. You'll need to update the config file though, to something along these lines:
<appSettings>
<DirectoryMappings>
<DirectoryMap key="MyBaseDir" value="C:\MyBase" />
<DirectoryMap key="Dir1" value="[MyBaseDir]\Dir1"/>
<DirectoryMap key="Dir2" value="[MyBaseDir]\Dir2"/>
</DirectoryMappings>
</appSettings>
I came up with this solution:
In the application Settings.settings I defined a variable ConfigurationBase (with type=string Scope=Application)
I introduced a variable in the target attributes in the Settings.settings, all those attributes had to be set to Scope=User
In the app.xaml.cs I read out the value if the ConfigurationBase
In the app.xaml.cs I replaced all variables with the ConfigurationBase value. In order to replace the values at run-time the attributes had to be set to Scopr=User
I'm not really happy with this solution because I have to change all attributes manually, if I add a new one I have to regard it in the app.xaml.cs.
Here a code snippet from the App.xaml.cs:
string configBase = Settings.Default.ConfigurationBase;
Settings.Default.CommonOutput_Directory = Settings.Default.CommonOutput_Directory.Replace("${ConfigurationBase}", configBase);
UPDATE
Just found an improvement (again a code snippet from the app.xaml.cs):
string configBase = Settings.Default.ConfigurationBase;
foreach (SettingsProperty settingsProperty in Settings.Default.Properties)
{
if (!settingsProperty.IsReadOnly && settings.Default[settingsProperty.Name] is string)
{
Settings.Default[settingsProperty.Name] = ((string)Settings.Default[settingsProperty.Name]).Replace("${ConfigurationBase}", configBase);
}
}
Now the replacements work for all attributes in my settings that have Type=string and Scope=User. I think I like it this way.
UPDATE2
Apparently setting Scope=Application is not required when running over the properties.
Three Possible Solutions
I know I'm coming late to the party, I've been looking if there were any new solutions to the variable configuration settings problem. There are a few answers that touch the solutions I have used in the past but most seem a bit convoluted. I thought I'd look at my old solutions and put the implementations together so that it might help people that are struggling with the same problem.
For this example I have used the following app setting in a console application:
<appSettings>
<add key="EnvironmentVariableExample" value="%BaseDir%\bin"/>
<add key="StaticClassExample" value="bin"/>
<add key="InterpollationExample" value="{0}bin"/>
</appSettings>
1. Use environment variables
I believe autocro autocro's answer touched on it. I'm just doing an implementation that should suffice when building or debugging without having to close visual studio. I have used this solution back in the day...
Create a pre-build event that will use the MSBuild variables
Warning: Use a variable that will not be replaced easily so use your project name or something similar as a variable name.
SETX BaseDir "$(ProjectDir)"
Reset variables; using something like the following:
Refresh Environment Variables on Stack Overflow
Use the setting in your code:
'
private void Test_Environment_Variables()
{
string BaseDir = ConfigurationManager.AppSettings["EnvironmentVariableExample"];
string ExpandedPath = Environment.ExpandEnvironmentVariables(BaseDir).Replace("\"", ""); //The function addes a " at the end of the variable
Console.WriteLine($"From within the C# Console Application {ExpandedPath}");
}
'
2. Use string interpolation:
Use the string.Format() function
`
private void Test_Interpollation()
{
string ConfigPath = ConfigurationManager.AppSettings["InterpollationExample"];
string SolutionPath = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, #"..\..\"));
string ExpandedPath = string.Format(ConfigPath, SolutionPath.ToString());
Console.WriteLine($"Using old interpollation {ExpandedPath}");
}
`
3. Using a static class, This is the solution I mostly use.
The implementation
`
private void Test_Static_Class()
{
Console.WriteLine($"Using a static config class {Configuration.BinPath}");
}
`
The static class
`
static class Configuration
{
public static string BinPath
{
get
{
string ConfigPath = ConfigurationManager.AppSettings["StaticClassExample"];
string SolutionPath = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, #"..\..\"));
return SolutionPath + ConfigPath;
}
}
}
`
Project Code:
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="EnvironmentVariableExample" value="%BaseDir%\bin"/>
<add key="StaticClassExample" value="bin"/>
<add key="InterpollationExample" value="{0}bin"/>
</appSettings>
</configuration>
Program.cs
using System;
using System.Configuration;
using System.IO;
namespace ConfigInterpollation
{
class Program
{
static void Main(string[] args)
{
new Console_Tests().Run_Tests();
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
internal class Console_Tests
{
public void Run_Tests()
{
Test_Environment_Variables();
Test_Interpollation();
Test_Static_Class();
}
private void Test_Environment_Variables()
{
string ConfigPath = ConfigurationManager.AppSettings["EnvironmentVariableExample"];
string ExpandedPath = Environment.ExpandEnvironmentVariables(ConfigPath).Replace("\"", "");
Console.WriteLine($"Using environment variables {ExpandedPath}");
}
private void Test_Interpollation()
{
string ConfigPath = ConfigurationManager.AppSettings["InterpollationExample"];
string SolutionPath = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, #"..\..\"));
string ExpandedPath = string.Format(ConfigPath, SolutionPath.ToString());
Console.WriteLine($"Using interpollation {ExpandedPath}");
}
private void Test_Static_Class()
{
Console.WriteLine($"Using a static config class {Configuration.BinPath}");
}
}
static class Configuration
{
public static string BinPath
{
get
{
string ConfigPath = ConfigurationManager.AppSettings["StaticClassExample"];
string SolutionPath = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, #"..\..\"));
return SolutionPath + ConfigPath;
}
}
}
}
Pre-build event:
Project Settings -> Build Events