Cannot read settings using ConfigurationManager [duplicate] - c#

I have a WinForms .exe with an App.config that has a bunch of User Scoped Settings that are set at runtime and saved.
I want to be able to use the WinForms app to change and save the settings and then click on button to do some work based on the those settings.
I also want to read the user settings in the same .config file from a sep. console app so I can schedule to work to be done as a scheduled task.
What is the best way to be able to do this?
Update:
I tried the reccommendation of using ConfigurationManager.OpenExeConfiguration as described in some of the answers like so.
Configuration config = ConfigurationManager.OpenExeConfiguration("F:\\Dir\\App.exe");
but when I try to retrieve a User Setting like so.
string result = config.AppSettings.Settings["DB"].ToString();
I get a Null reference error.
From code in the exe however the following correctly returns the DB name.
Properties.Settings.Default.DB
Where am I going wrong?
Update 2:
So based on some of the answers below I now can use the following to retrieve the raw XML of the section of the user.config file I am interested in from sep. ConsoleApp.
System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = #"D:\PathHere\user.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None);
System.Configuration.DefaultSection configSection = (System.Configuration.DefaultSection)config.GetSection("userSettings");
string result = configSection.SectionInformation.GetRawXml();
Console.WriteLine(result);
But I am still unable to just pull the value for the specific element I am interested in.

this should do it :
var clientSettingsSection = (System.Configuration.ClientSettingsSection)(ConfigurationManager.GetSection("userSettings/YourApplicationName.Properties.Settings"));
var setting = clientSettingsSection.Settings.Get("yourParamName_DB_");
string yourParamName_DB = ((setting.Value.ValueXml).LastChild).InnerText.ToString();

You can use the ConfigurationManager class to open the configuration file of another executable.
Configuration conf = ConfigurationManager.OpenExeConfiguration(exeFilePath);
// edit configuration settings
conf.Save();

See ConfigurationManager.OpenExeConfiguration

If you are setting these values per user basis you may need to use the ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel) method instead of the current method you are using now.

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Environment.CurrentDirectory + "\\MyApp.exe.config";
//i have config in the same directory as my another app
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var settingsSection = (System.Configuration.ClientSettingsSection)config.GetSection("userSettings/MyApp.Properties.Settings");
var setting = settingsSection.Settings.Get("MySetting");
var param = ((setting.Value.ValueXml).LastChild).InnerText.ToString();

If you know the path to the config file try:
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration("configPath");
string myValue = config.AppSettings.Settings[ "myValue" ].Value;

Related

Why does XmlDocument read .config file, but ConfigurationManager does not? [duplicate]

I have added multiple app.config (each with a differet name) files to a project, and set them to copy to the output directory on each build.
I try and access the contents of each file using this:
System.Configuration.Configuration o = ConfigurationManager.OpenExeConfiguration(#"app1.config");
The code runs, but o.HasFile ends up False, and o.FilePath ends up "app1.config.config". If I change to code:
System.Configuration.Configuration o = ConfigurationManager.OpenExeConfiguration(#"app1");
Then the code bombs with "An error occurred loading a configuration file: The parameter 'exePath' is invalid. Parameter name: exePath".
If I copy/paste the config file (so I end up with app1.config and app1.config.config) then the code runs fine, however, I posit this is not a good solution. My question is thus: how can I use ConfigurationManager.OpenExeConfiguration to load a config file corretly?
I can't remember where I found this solution but here is how I managed to load a specific exe configuration file:
ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = "EXECONFIG_PATH" };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
According to http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/3943ec30-8be5-4f12-9667-3b812f711fc9 the parameter is the location of an exe, and the method then looks for the config corresponding to that exe (I guess the parameter name of exePath makes sense now!).
To avoid this problem altogether, you can read in the config file as an XML file, for example:
using System.Xml;
using System.Xml.XPath;
XmlDocument doc = new XmlDocument();
doc.Load(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\..\\..\\..\\MyWebProject\\web.config");
string value = doc.DocumentElement.SelectSingleNode("/configuration/appSettings/add[#key='MyKeyName']").Attributes["value"].Value;
using System.Reflection;
try
{
Uri UriAssemblyFolder = new Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
string appPath = UriAssemblyFolder.LocalPath;
//Open the configuration file and retrieve
//the connectionStrings section.
Configuration config = ConfigurationManager.
OpenExeConfiguration(appPath + #"\" + exeConfigName);
ConnectionStringsSection section =
config.GetSection("connectionStrings")
as ConnectionStringsSection;
}
At least, this is the method I use when encrypting and decrypting the connectionStrings section for my console/GUI apps. exeConfigName is the name of the executable including the .exe.

How to access "user.config" of different application

The user.config file of an application can be accessed in C# by
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath
Now, I need to get the user.config of a DIFFERNT application, identified by the fullpath to its exe in order replace current applications user.config by that one.
Any suggestions how to do so?
Edit: Please notice that I am interested in the user.config (ConfigurationUserLevel.PerUserRoamingAndLocal), not the application level settings like app.config or Application.exe.config. The latter is accessable by the OpenExeConfiguration(string), but is not what I want.
What about the overload that accepts a string?
var config = ConfigurationManager.OpenExeConfiguration(pathToAssebmly);
Anyway it´s a bad idea to bind your assembly that way to another one. You should consider to copy the config-file into all your assemblies and change the parts that are specific to a given one.
EDIT: As you´re interested in the user-specific setting you may use the following which is taken from here:
string configFile = string.Concat(appName, ".config");
// Map the new configuration file.
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap
{
ExeConfigFilename = configFile
};
var config = ConfigurationManager.OpenMappedExeConfiguration(
configFileMap,
ConfigurationUserLevel.PerUserRoamingAndLocal);

Replace, not add the value in .config file

I have a Windows Service written, and want to modify it's config file. I have an app in the same solution for this:
private ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = #"pathToMyWSConfigFile";
and then I do:
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("ServerPath", "test replace ");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
However this simply adds another value to the ServerPath key in config file, and I want to replace it, so that there is always only one value there. How can I do it?
To update a setting, simply use the Settings indexer.
config.AppSettings.Settings["ServerPath"].Value = "test replace";

Writing appSetting to separate *.config file

I have a self-hosted web api project, which appSettings are stored in separate location. I've achieved this by using file=""
<appSettings file="D:\myLocation\api.config">
</appSettings>
Then I'm able to read my settings using this code:
var value = ConfigurationManager.AppSettings["MyKey"];
But when I try to re-write the value, using this code:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["MyKey"].Value = "someNewValue"
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
I'm getting original app.config file updated, instead of updating my separate stored file.
Is there any way of updating external appSettings file programmatically? Or maybe there are other best practices of storing settings separately?
Please, ask, if you need more information!
Thank you in advance!
Update
Just to prevent comments and answers like "wouldn't it be easier..." - this service is going to work on specific system with Enhanced Write Filter applied. This means, I won't be able to make any persistent changes on the partition. That's why I'm looking for a way to store settings in separate file located on separate partition without EWF. This scenario puts strict limits on how I can resolve the issue. Thank you for understanding!
Why not open the external config file as a regular Xml file and write your changes? This way you dont have to worry about anything else.
Specify the name of the config section like this:
config.AppSettings.SectionInformation.ConfigSource = "api.config";
Use below.
var configFile = new FileInfo("D:\WebsiteABC\Web.config");
var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
var wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/", vdm);
configuration = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
configuration.AppSettings.SectionInformation.ConfigSource = "AppSettings.config";

Modify Database connection string of web.config..?

Good day! I would like to ask, if you know how to modify the web.config.. Its located in a different folder.. I tried using this approach, Unfortunately i doesn't work..
// set Path to your config file
System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(sWebConfig);
// open web.config
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
// display message
Console.WriteLine("Updating wizardConnection string.. Please wait for a few minutes..");
// fetch WizardConnection database connection string
var Wizardsection = (ConnectionStringsSection)configuration.GetSection("WizardConnection");
// assign new value to wizardConnection.. Please make sure you have the correct database server. Just update server location, if need
Wizardsection.ConnectionStrings["WizardConnection"].ConnectionString = string.Format(#"server={0};database={1};integrated security=SSPI", sDatabaseServer, sDatabase);
configuration.Save();
Hope to hear from you soon..
Regards,
Link
Instead of updating web.config, you may use different web.config files for different build configurations. For instance, you may setup a new build configuration namely "Staging" and configure it to use a modified config file.
Have a look at Web.Config transformation
Actually what you are doing will change the config file which is copied to folder that contains binary(debug, release or any custom compiler configuration). That is sufficient to change the connection settings of the application. But, if you want to modify the web.config file, it will be nothing but modifying an external file, for which you will need File opertaions.
You should use webconfigurationmanager to open your webconfig.
Try this
var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
config.ConnectionStrings.ConnectionStrings["ConnectString"].ConnectionString = string.Format(#"server={0};database={1};integrated security=SSPI", sDatabaseServer, sDatabase);
config.Save();

Categories