read from app.config instead of dll.config - c#

I have simple console application. In application there is app.config and i have defined few settings which i fetch using ConfigurationManager class like below
var setting = ConfigurationManager.AppSettings[key]
This works ok when i am debugging on local env. Now when i deploy this code to develepment server then it reads setting from dll.config instead of app.config.
I tried to google but i am not able to find any clue.
Is it possible to change behaviour to read from app.config always? The reason i am asking is because i have added transformation for app.config. So for Dev server its called app.DevServer.config and it has some specific settings.

Yes, you can manually read your app.config file with code like this:
var configMap = new ExeConfigurationFileMap { ExeConfigFilename = "app.DevServer.config" };
var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None, true);
var setting = config.AppSettings.Settings[key];

Related

write to a different programs app.config c#

I am needing to find away to write to another programs app.config file.
we have a 3rd party application that gets some values from an AppSettings section in its own config. but the only way we can get to change the value is when we call the application and once it runs it does a function which we don't want it to do if it doesn't have the correct values.
We need to encrypt one of the values so what we were think of was creating an application that would save the values in this 3rd party app.config.
saving the values and the encryption aren't a problem its doing it in another applications config file.
Is there a way to set which config file we use or a config path setting?
Regards
Aidan
Thank you for the help with this, I managed to get it done through this code.
string configLocation = string.Format("{0}\\APP.exe.config", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = configLocation;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
txtEndpoint.Text = config.AppSettings.Settings["ENDPOINT"].Value;
This now allows me to write out the set config file values in the appsettings.
Cheers All.
Aidan

ConfigurationManager.RefreshSection not working in c# windows service

I am having two projects one is basically a Windows service and second is class project in which I am doing my business processing. My App.config file is in Windows service project and in my Class project I am using below
ConfigurationManager.RefreshSection("appsettings");
string scheduledTime = ConfigurationManager.AppSettings["ScheduleTime"];
this setting is in my appsettings section of config file
I am using RefreshSection and also updating my app.config value in windows service project but its not getting updated at run time in my class project.
What is the catch in this ?
I'm having a little trouble myself with something similar, however I did come across something for AppSettings. Give this a shot:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
I think appsettings should be appSettings
If you want to get latest value use this code:
var appSettings = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location).AppSettings;
var mySetting = appSettings.Settings["keyOfSetting"].Value;
remember every time you need latest value, must use two line of code in same place!

App.Config not updating

I want to update app. config file in windows form c# . here is a code for updating app. config
// updating
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove("name");
config.AppSettings.Settings.Add("name",txtName.Text);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
// show updating result on labels ( and this work fine )
string value = ConfigurationManager.AppSettings["name"];
lblName.Text = value;
this update work fine when I am running an application but when I restart application all config is reset to default
Instead of using the Configuration class, open the file as a regular Xml file and make your changes. Note that when you are doing this, when you save the file back, any Comments in your .config file will be removed by the Xml class. To prevent this, you must read ALL types of Xml nodes from the original configuration file and write them all back.

Project returns "object reference not set..." when trying to read from app.config

I have a c# CLR project that is called by MS SQL Server. The CLR project tries to connect to, and read from, a WCF service.
Normally the webservice bindings and endpoints are configured in the corresponding app.config file. This is also the case in my project.
However, when I, for example, try to read the AppSettings from the app.config file I get a "object reference not set to an instance of this object". The return value from the AppSettings call is null. The CLR can't find the config file. I want to call the config file because it contains the endpoint URL I want to connect to.
This is what I'm doing:
1. Building the CLR DLL and putting it in a directory together with the configuration file
2. Loading the assembly in MS SQL Server (2008 R2)
3. Creating functions and launching a call to the CLR
What am I doing wrong? Is there any way to read from the app.config file in this particular scenario?
Hope the question is clear.
Thanks in advance.
How are you reading your settings from the app.config file?
Remember to use the ConfigurationManager class, available in the System.Configuration namespace, to retrieve the value of your setting in the AppSettings section of configuration file. You can do that in this way:
var value = ConfigurationManager.AppSettings["mySetting"];
if you have your app.config file like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="mySetting" value="setting"/>
</appSettings>
</configuration>
Maybe the simplest solution would be to configure WCF client programatically within CLR DLL library?
var binding = new System.ServiceModel.BasicHttpBinding();
binding.SendTimeout = TimeSpan.FromMinutes( 1 );
binding.OpenTimeout = TimeSpan.FromMinutes( 1 );
binding.CloseTimeout = TimeSpan.FromMinutes( 1 );
binding.ReceiveTimeout = TimeSpan.FromMinutes( 10 );
using (var client = new SomeServiceSoapClient(binding, new System.ServiceModel.EndpointAddress("http://site.example/SomeService.asmx")))
{
// client.DoWork(...)
}
and pass configuration data (URL) as argument to the SQL CLR function? In this way you are independent from the app.config location problems.

Open other program's configuration files

I have a program A, it also have an app.config file where I have added some keys like server address, username and password for connecting to a server. It is a console application. Now I want to make a UI which I have done. In that UI I want to modify the content of app.config of program A. How do I do that?
Here is what I tried, I copied the UI (basically an .exe) to program A's directory, where the app.config also resides. Then in the UI, I use the ConfigurationManager class's OpenExeConfiguration method, and passing the program A's filename as an argument. But it throws and exception of type System.Configuration.ConfigurationErrorsException.
So I think that my approach is incorrect. How shall I do this?
EDIT: Oh I forgot to tell I'm using C#, .NET 3.5 and VS 2008 (if that helps :D)
I'm not sure about the problem with your approach (try adding the stack trace to your post) but this is how I do it:
var configMap =
new ExeConfigurationFileMap
{
ExeConfigFilename = externalConfigurationFile
};
System.Configuration.Configuration externalConfiguration =
ConfigurationManager.OpenMappedExeConfiguration(
configMap,
ConfigurationUserLevel.None);
foreach (var setting in externalConfiguration.AppSettings.Settings)
{
...
}
externalConfiguration.Save(ConfigurationSaveMode.Full);

Categories