Add hardcoded configuration without app.config file to some assembly - c#

I need to add configuration information to the assembly itself without including its app.config file with it.
How can i do it?
EDIT:
I need something like this
string config = #"<?xml version='1.0' encoding='utf-8'?>
<configuration>
.
.
.
.
</configuration>";
Set this hardcoded string configuration as current assembly configuration

Configuration settings be it user or application settings have their default values "hardcoded" in the assembly by default. They can be overriden by including an app.config, or modifying user settings at runtime and saving to the user config file.
After creating project settings (in project properties and go to the "Settings" tab), a Settings class is generated with static properties which will have the default value you configured.
They are accessible throughout your assembly like so:
Assert.AreEqual(Properties.Settings.MySetting, "MyDefaultValue");
These default values can be overridden via the app.config:
<applicationSettings>
<MyProject.Properties.Settings>
<setting name="MySetting" serializeAs="String">
<value>MyDefaultValue</value>
</setting>
</MyProject.Properties.Settings>
</applicationSettings>
To answer your question: You can omit including the app.config from your application deployment, the defaults that you provided when configuring the settings are hardcoded.
Edit:
Just noticed you actually want to read the entire app.config from your assembly.
A possible approach could be the following:
// 1. Create a temporary file
string fileName = Path.GetTempFileName();
// 2. Write the contents of your app.config to that file
File.WriteAllText(fileName, Properties.Settings.Default.DefaultConfiguration);
// 3. Set the default configuration file for this application to that file
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", fileName);
// 4. Refresh the sections you wish to reload
ConfigurationManager.RefreshSection("AppSettings");
ConfigurationManager.RefreshSection("connectionStrings");
// ...

you can have a custom XML file where you store your settings and then you set it as Embedded resource so it will be available inside the exe or dll.
see here: How can I retrieve an embedded xml resource? for an example on how to read it at runtime
Edit: and to load it as custom configuration file check here: Loading custom configuration files

Related

How can I use several Application Configuration Files in one project?

After creating new Visual C# Console Application (.NET Framework 4.5), such project contains default App.config file.
After adding a reference for System.Configuration to project and use it in some source file using System.Configuration; I can use static class ConfigurationManager to operate with App.config file. But before, I want to add some settings to the file, so it's somehow like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DeployWeb" value="true" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Now, I can write something like this, to get the value of the setting:
Boolean deployWeb = false;
Boolean.TryParse(ConfigurationManager.AppSettings["DeployWeb"], out deployWeb);
However I didn't set which configuration file to read,but it's okay, because there is the default one. But I can add more configuration files by right click on the project -> Add -> New Item... -> Application Configuration File, so that I have, for example, 5 configuration files, like on the picture:
And ConfigurationManager will still read the default one, but I want to manually control, which config file to use. I want to know, if is there an appropriate way to set to the ConfigurationManager config file name to use etc. and if it's not a bad practice. I know how to use different configurations in debug/release mode, but in my case I have an application, that can be runned in different modes for different purposes in release, for example.
Question: Is it possible to have several configuration files in a project and to have ability to switch which one I want to use. Isn't it a bad practice, shall I use some another approach for my purpose ? Using build events is not suitable in my case (I think).
PS. I am sorry for stretching my question, however my itis quite simple and could be just asked in two sentences, but as the rules says, question should contains details.
UPDATE:
From already existing answer "Option: You can use the ConfigurationManager Class to load an alternate config file by code." From reading msdn I didn't get which of the methods should I use. Should I open Exe configuration ?
It can be done using mapped config file ,
ExeConfigurationFileMap configFileMap =
new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "App4.config"; // full path to the config file
// Get the mapped configuration file
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(
configFileMap, ConfigurationUserLevel.None);
//now on use config object
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");

Does the appsettings file attribute override what is in the app.config?

The appsettings tag in the app.config has a file attribute:
<appSettings file="other.config">
..
..
</appSettings>
How does this work? Will it merge what is in the appSettings (original) with the other.config file? Or will it overwrite it? What if the other.config file doesn't exist, should it crash?
I'm trying it myself and if a key isn't in the original, it doesn't seem to read it from the other.config?
Should the other.config file have just xml nodes, or should it all be inside a appsettings element?
<appSettings>
<userId>123</userId>
</appSettings>
or
<userId>123</userId>
If the file doesn't exist it will not crash, it will just be ignored.
The external config has to contain the <appSettings> node so your first example is correct.
The value in the external file will take priority, if no value is present then the app.config value is used.
Does that cover off everything?
One of the best answers on the subject is here: ASP.NET web.config: configSource vs. file attributes - Credit to #Massimiliano Peluso
file attribute
Specifies a relative path to an external file that contains custom application configuration settings
specific to the appSettings section
will merge (and override) settings in the .config file
will not cause web application to restart when modifying the specified file
http://msdn.microsoft.com/en-US/library/ms228154(v=vs.100).aspx
Using the Configuration.AppSettings.Settings.Add API will result in all settings being merged back into the main .config on a Configuration.Save call.
since .NET 1.1
Exception is not thrown if file does not exist.
configSource attribute
can apply to most sections of a configuration file, not just appSettings
will override the entire section with the external file, no merging
CAN cause web application to restart
http://msdn.microsoft.com/en-US/library/system.configuration.sectioninformation.configsource(v=vs.100).aspx
Using the Configuration.AppSettings.Settings.Add API will result in all settings being added to the file specified in configSource on a Configuration.Save call.
since .NET 2.0
System.Configuration.ConfigurationErrorsException is thrown if config source file does not exist.
The file attribute specifies an external file containing custom settings like you do in the appSettings entry of the web.config file.
Meanwhile, the external file specified in the configSource attribute contains the settings for the section which you declare the configSource for. For example, if you use the configSource attribute of the pages section, then the external file will contain the settings for the pages section.
The custom settings declared in the external config specifified in the
file attribute will be merged with the settings in the appSettings
section in the web.config file. In the meanwhile, the configSource
does not support merging, it means that you'll have to move the entire
section settings into the external file.
http://www.codeproject.com/Messages/1463547/Re-difference-between-configSource-and-file-attrib.aspx

Where are config files for class libraries physically located?

My guess is that this question will fall under the category of "duh", but, nevertheless, I'm confused.
When using config files in, for example, a Windows Forms Application, the config file can be found in C:\Program files\CompanyName\ProductName\Application.exe.config. However, with the class library I'm developing I do not see a "ClassLibrary.dll.config" file in the install folder after installing it (in tandem with another project) from Visual Studio. Even though I do not see the file anywhere, retrieving data from it works correctly. Plus, running the following code from a method within the class library returns the path you would expect: C:\Program files\CompanyName\ProductName\ClassLibrary.dll.config.
If someone could shed some light on what I'm missing here, that would be really awesome.
public static string MyMethod()
{
Assembly assem = Assembly.GetExecutingAssembly();
Configuration config = ConfigurationManager.OpenExeConfiguration(assem.Location);
return "The assembly location was: " + assem.Location + Environment.NewLine +
"The config file path was: " + config.FilePath;
// Gives me "C:\Program files\CompanyName\ProductName\ClassLibrary.dll.config"
}
In a class library, the app.config file is pretty useless. Application setting values get stored in the Settings.settings file and are compiled into the Settings.designer.cs file.
If you modify the value of one of your application settings in app.config directly, that won't have an effect on the setting value seen while running the application. You have to actually open the Settings editor, at which point it will notice the difference between the app.config file and the Settings.settings file and inquire as to whether you'd like it to update your Settings.settings file using values from app.config.
Depends on the version of .NET. Prior to .NET 4.0, class libraries cannot use their own app.config files unless you do custom stuff to read the data in. In these scenarios, you should have the initialization of your class libraries require all of the appropriate data and pass that in from your consuming class's configuration (i.e. ultimately your winform's app.config file).
Your class library will use the config file of the executable that called it.
Your default value is stored in the dll.
But let's say you change the setting in code and save it. Where does it get stored for next time?
In WinXP look in:
C:\Documents and Settings\username\Local Settings\Application Data\
or in Windows Vista/7 look in:
C:\Users\username\AppData\Local\
You will find a folder named after your application and drilling down in to that folder, you will find a file named user.config.
For example:
C:\Documents and Settings\username\Local Settings\Application Data\MyApp\myapp.exe_urlla1ii3sytrhx0adqtjnjuc24oacqpgu4\1.0.0.0\user.config
Just to expand on #TLiebe's answer here, it is stored in the main assembly's settings file under a separate domain. For example:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<TestLib.Properties.Settings>
<setting name="TestSetting" serializeAs="String">
<value>2.8.0.0</value>
</setting>
</TestLib.Properties.Settings>
<UpdateTest.Properties.Settings>
<setting name="Token" serializeAs="String">
<value>{fV234hwolihj453lvB}</value>
</setting>
</UpdateTest.Properties.Settings>
</userSettings>
</configuration>

Relocating app.config file to a custom path

Is it possible to relocate the whole App.Config file to a custom path?
It seems a bit odd that the config file resides in the same folder as the exe, with Windows' new approcah of saving all program settings in c:\ProgramData and all.
An additional requirement we have is to programatically specify where to find the app.config file. The reason for this being that we spawn different service instances from the same exes, and would like to store each service's app.config in that service's settings folder under c:\ProgramData\\.
If still relevant, we have used the following I found on another suggested answer to another question here on Stack Overflow...
AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file")
Worked great for us when we had issues loading app.config from DLL only...
Each AppDomain has/can have its own configuration file. The default AppDomain created by CLR host uses programname.exe.config; if you want to provide your own configuration file, create separate AppDomain. Example:
// get the name of the assembly
string exeAssembly = Assembly.GetEntryAssembly().FullName;
// setup - there you put the path to the config file
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = System.Environment.CurrentDirectory;
setup.ConfigurationFile = "<path to your config file>";
// create the app domain
AppDomain appDomain = AppDomain.CreateDomain("My AppDomain", null, setup);
// create proxy used to call the startup method
YourStartupClass proxy = (YourStartupClass)appDomain.CreateInstanceAndUnwrap(
exeAssembly, typeof(YourStartupClass).FullName);
// call the startup method - something like alternative main()
proxy.StartupMethod();
// in the end, unload the domain
AppDomain.Unload(appDomain);
Hope that helps.
I know this is an old question, but for those who just want to have their app.config in a different location then their binary output build location, the following works as microsoft intended it (and thus no need re-read and re-write the file to disk)
Define an app.config as you always do.
Define another config file where you want to have the actual configuration file
Change the app.config so that it refers to the configuration file
At runtime the settings from the configuration file will override the settings in the app.config (if present). And you are done.
Example app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<appSettings file="..\Config\settings.config">
<add key="port" value="1001"/>
</appSettings>
</configuration>
Note the file="..\Config\settings.config". You are completely free to define the path to the location where you want your users to change settings.
Example of the actual configuration file
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="port" value="1234"/>
</appSettings>
At runtime the setting port will have the value 1234.
More info see msdn
This worked for me.. (taken from http://msdn.microsoft.com/en-us/library/system.configuration.appsettingssection.aspx)
// open config
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// update appconfig file path
config.AppSettings.File = "C:\\dev\\App.config";
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload in memory of the changed section.
ConfigurationManager.RefreshSection("appSettings");
Then when you call
NameValueCollection settings = System.Configuration.ConfigurationManager.AppSettings;
or any operation to fetch the app config, the new path is used.
Hope this might help someone else who has same problem!
I am sorry if I misunderstand your request but can you not use
ConfigurationManager.OpenExeConfiguration Method (String)
Based on the change, is it possible that you can use
AppDomainSetup.ConfigurationFile Property
This is an ancient question, but I ran into this same problem and came up with a hacky workaround from a few minutes in reflector:
static public class ConfigHack {
static public void OverrideAppConfig(string path) {
((AppDomainSetup)
typeof(AppDomain)
.GetField("_FusionStore", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(AppDomain.CurrentDomain))
.ConfigurationFile = path;
}
static public void ResetConfigManager() {
typeof(ConfigurationManager)
.GetField("s_initState", BindingFlags.Static | BindingFlags.NonPublic)
.SetValue(null, 0);
}
}
I've only used it on .NET2, but it looks the same in 4 in reflector. Of course I wouldn't recommend shipping this :P I only use it for quick internal things.
You could use astander's approach of calling OpenExeConfiguration. If you literally want to relocate your config file, you'll have to create your own app domain. In the process of setting up your app domain you get the chance to specify where the config file is located.
BTW, .NET config files aren't great for configuration, at least not the sort that users can modify: they're not like INI files or the registry. If you want flexibility over where your configuration comes from you're better off storing it separately.
If u use the registry, your problem can be resolved.
MSDN probably would help...
The element
simplifies servicing for component
assemblies. If one or more
applications use an assembly that has
a configuration file residing in a
well-known location, the configuration
files of the applications that use the
assembly can use the
element to
include the assembly configuration
file, rather than including
configuration information directly.
When the component assembly is
serviced, updating the common
configuration file provides updated
configuration information to all
applications that use the assembly

App.config for dll

We have an "engine" that loads dlls dynamically (whatever is located in a certain directory) and calls Workflow classes from them by way of reflection.
We now have some new Workflows that require access to a database, so I figured that I would put a config file in the dll directory.
But for some reason my Workflows just don't see the config file.
<configuration>
<appSettings>
<add key="ConnectString" value="Data Source=officeserver;Database=mydatabase;User ID=officeuser;Password=officeuser;" />
</appSettings>
</configuration>
Given the above config file, the following code prints an empty string:
Console.WriteLine(ConfigurationManager.AppSettings["ConnectString"]);
I think what I want is to just specify a config filename, but I'm having problems here. I'm just not getting results.
Anyone have any pointers?
If your code sample for reading the AppSettings is in your DLL, then it will attempt to read the config file for the application and not the config file for the DLL. This is because you're using Reflection to execute the code.
Funny, where I'm at we're doing something very similar and the config file loads just fine. In our case I think each new config file's name matches that of it's associated assembly. So MyLibrary.dll would have a file named MyLibrary.dll.config with information for that file assembly. Also, the example I have handy is using VB.Net rather than C# (we have some of each) and all the settings in there are for the VB-specific My.Settings namespace, so we don't use the ConfigurationManager class directly to read them.
The settings themselves look like this:
<applicationSettings>
<MyLibrary.My.MySettings>
<setting name="SomeSetting" serializeAs="String">
<value>12345</value>
</setting>
</MyLibrary.My.MySettings>
</applicationSettings>
I wrote this for a similar system. My recollection is that I used Assembly.GetExecutingAssembly to get the file path to the DLL, appended .config to that name, loaded it as an XmlDocument, navigated to the <appSettings> node and passed that to a NameValueSectionHandler's Create method.
Here is one way -
AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file");
Call in constructor.
If I recall correctly, the app.config will be loaded from your application directory, so if you are loading dlls from some other directory, you'll want the keys they need in your application's config file.
I'm not totally sure but I think that class only works with the path of the entry method of the AppDomain (the path of the exe most of the time) by default.
You need to call OpenExeConfiguration(string exePath) (Framework 2.0 and later) first to point to a different config file.

Categories

Resources