I've tried updating my application's .config file during installer (via a .NET Installer Class Action). But, I can't seem to get ConfigurationManager to list any properties or be able to set anything.
I learned of this approach from several stackoverflow posts that pointed me to this guide: http://raquila.com/software/configure-app-config-application-settings-during-msi-install/
I've investigated the differences between my project and his and noticed that my configuration files are formatted differently. I believe this has to do with the fact that i'm using "Settings" files.
Config files formatted in the guide look like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Param1" value="" />
<add key="Param2" value="" />
<add key="Param3" value="" />
</appSettings>
</configuration>
Where mine looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MyAppName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="MyAppName.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MyAppName.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<MyAppName.Properties.Settings>
<setting name="TESTSETTING" serializeAs="String">
<value>asdfasdfasdf</value>
</setting>
</MyAppName.Properties.Settings>
<MyAppName.Settings1>
<setting name="VerboseErrorMode" serializeAs="String">
<value>False</value>
</setting>
<applicationSettings>
<MyAppName.Settings1>
<setting name="RunOnStartup" serializeAs="String">
<value>True</value>
</setting>
</MyAppName.Settings1>
</applicationSettings>
</configuration>
To shed some light on what was going on... I tried printing out the list of settings like so:
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
// Try getting the Settings1 Section
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("Settings1"); // Also tried myNamespace.Settings1
if (appSettings != null)
{
valList = "Settings1: ";
foreach (string key in appSettings.Settings.AllKeys)
{
string value = appSettings.Settings[key].Value;
valList += ("Key: '" + key + "' = '" + value + "'\n");
}
}
else
{
valList = "appSettings was null";
}
MessageBox.Show(valList);
MessageBox.Show(valList);
I have tried several permutations of this... and in all cases output is "appSettings was null".
I also tried initializing the configuration manager in several different ways...
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
MessageBox.Show("Section Count: " + config.Sections.Count);
MessageBox.Show("Has File: " + config.HasFile);
MessageBox.Show("Namespace Declared: " + config.NamespaceDeclared);
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MessageBox.Show("Section Count: " + config.Sections.Count);
MessageBox.Show("Has File: " + config.HasFile);
MessageBox.Show("Namespace Declared: " + config.NamespaceDeclared);
For each of them the section count returned was 20. (I have no idea where the 20 comes from... I would have expected it to be 3).
HasFile was true for the first case and false for the second.
Namespace Declared was false in both cases.
Thanks!
EDIT (6-18-09): Still looking into this question. Anyone else have any ideas? Thanks.
Search Keywords: "Object Reference Not Set to not set to an instance" <-- this occurs when trying to write to a property.
I came across the same problem, after deep investigation, I found out the easiest way to update any part of config files (e.g. app.config), that's by using XPath.
We have an application which connects to web service, during the installation, the user enters the URL of the web service and this should be saved in the following app.config file:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="ApplicationServer.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<ApplicationServer.Properties.Settings>
<setting name="ApplicationServer_ApplicationServerUrl" serializeAs="String">
<value>whatever comes from setup should go here</value>
</setting>
</ApplicationServer.Properties.Settings>
</applicationSettings>
</configuration>
Here is the code to do this in the installer class:
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
string targetDirectory = Context.Parameters["targetdir"];
string param1 = Context.Parameters["param1"];
string path = System.IO.Path.Combine(targetDirectory, "app.config");
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(path);
System.Xml.XmlNode node = xDoc.SelectSingleNode("/configuration/applicationSettings/Intellisense.ApplicationServer.Properties.Settings/setting[#name='ApplicationServer_ApplicationServerUrl']/value");
node.InnerText = (param1.EndsWith("/") ? param1 : param1 + "/");
xDoc.Save(path); // saves the web.config file
}
Basically, since the config file is a XML based document, I am using XPath expression to locate specific node and change its value.
One thing to try is moving it from install to Commit to ensure that the file has been written first before trying to access it. Alternatively you could use the custom action to write your own file and just alter your config file to point at the alternative xml file.
I worked on a Sharepoint product install where I dug into all of this and I will admit it is very tedious to get working correctly. I was creating config and batch files on the fly based on install parameters.
You can access these settings using the System.Configuration namespace but, its not as simple as I would like and in retrospect using System.Xml.Linq is far simpler. Anyway, here is how I got it to work.
The important concept is, the applicationSettings section is not AppSettings, its a seperate section supported by the ClientSettingsSection type.
//Open the application level config file
ExeConfigurationFileMap exeMap = new ExeConfigurationFileMap();
exeMap.ExeConfigFilename = String.Format("{0}.config",
Context.Parameters["assemblypath"]);
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap,
ConfigurationUserLevel.None);
//Get the settings section
ClientSettingsSection settingsSection =
config.GetSectionGroup("applicationSettings").Sections
.OfType<ClientSettingsSection>().Single();
//Update "TheSetting"
//I couldn't get the changes to persist unless
//I removed then readded the element.
SettingElement oldElement = settingsSection.Get("TheSetting");
settingsSection.Settings.Remove(oldElement);
SettingElement newElement = new SettingElement("TheSetting",
SettingSerializeAs.String);
newElement.Value = new SettingValueElement();
newElement.Value.ValueXml = oldElement.Value.ValueXml.CloneNode(true);
newElement.Value.ValueXml.InnerText = "Some New Value";
settingsSection.Add(newElement);
//Save the changes
config.Save(ConfigurationSaveMode.Full);
So, as you can see, simple. :-S
Instead of accessing your settings configuration via ConfigurationManager, you should be able to access it via Settings.Default. Settings are more of a Visual Studio feature than a .NET feature...a convenience that makes it easy to visually design your applications configuration rather than manually writing it in appSettings or creating custom configuration sections. However, the configuration schema that is rendered when you use Settings is non-standard, and can be difficult to access manually.
Visual Studio should have generated a Settings class for you when you built your application, and you should be able to access that class via Properties.Settings.Default. It should have a property for each setting, in your case, the following:
Properties.Settings.Default.TESTSETTING
Properties.Settings.Default.VerboseErrorMode
Properties.Settings.Default.RunOnStartup
You should be able to both read and write these settings. A critical thing to note...anything flagged as a "user" setting will not be written back to the {yourapplication}.exe.config file...it will be written to a User.config file in the users isolated profile storage area. This is under C:\Documents and Settings{username} on XP, and C:\Users{username} on Vista, in the AppData folder. Depending on the OS and users profile, the subfolder under the AppData may change, but its completely unique and keyed to a particular version of the application. Installation of a newer version will result in a completely new set of configuration settings stored under the same keyed folder, but a different version subfolder.
I hope this helps. :)
This is what you need: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/f89a00eb-9400-48ce-af20-cef78002c14e
Related
I have a app.config file.
I have settings there.
I want to modify settings after my build and then read it from file.
I mean I want to change settings by edeting a file.
I know how to change settings programmaticaly but I need it by editing file.
No I'm trying to do so:
private void ReadSettings()
{
string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string settingsPath = Path.Combine(appPath, "app.config");
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = settingsPath;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
string value=config.AppSettings.Settings["Type"].Value;
}
My settings:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="RxTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<MyApp.Properties.Settings>
<setting name="Type" serializeAs="String">
<value>document</value>
</setting>
</RxTest.Properties.Settings>
</userSettings>
</configuration>
Problem is that there is nothing in Settings property.
What shuld I do to read settings from edeted file?
The UserSettings written in your app.config file should be automatically added into it by adding an entry into your properties.
You can achieve getting the values from your properties like this:
string value = Properties.Settings.Default.Type;
EDIT I:
To be sure you always have the freshest value, you can either refresh a section:
ConfigurationManager.RefreshSection(sectionName);
Or you can reaload the file:
Properties.Settings.Default.Reload();
I have a console application written in C# which uses an app.config file. This application is intended to be run on a server using the task scheduler. Now I want to develop a UI that reads and writes from and to the app.config. (Note that this config is not intended to replace the config file of the UI application.)
But I'm struggling to read the settings from the file. Using the ConfigurationManager I'm able to open the config file, BUT I cannot access the configuration settings.
This is the sample config file generated by Visual Studio (2010):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AccessingConfigSample.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<AccessingConfigSample.Properties.Settings>
<setting name="ApplicationTitle" serializeAs="String">
<value>Accessing Config files</value>
</setting>
<setting name="VersionNo" serializeAs="String">
<value>V 1.0</value>
</setting>
</AccessingConfigSample.Properties.Settings>
</userSettings>
</configuration>
After consulting several article on stackoverflow, I tried this to open the file and access the user section:
if (File.Exists(configFile))
{
var configMap = new ExeConfigurationFileMap{ ExeConfigFilename = configFile};
var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
var userSection = config.GetSection("userSettings");
}
I tried this as well:
var userSection = config.GetSection("AccessingConfigSample.Properties.Settings");
Both returned null.
So what am I doing wrong here?
Any help or hints are highly appreciated!
The config file you use as an example is using a ConfigurationSectionGroup and those need to be read with the matching method GetSectionGroup on the Configuration element instead of GetSection
The following code snippet does output the content of the SectionGroup to the Debug console:
if (File.Exists(configFile))
{
var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
// get the sectionGroup!
var userSectionGroup = config.GetSectionGroup("userSettings");
foreach (var userSection in userSectionGroup.Sections)
{
// check for a ClientSettingSection
if (userSection is ClientSettingsSection)
{
// cast from ConfigSection to a more specialized type
var clientSettingSect = (ClientSettingsSection) userSection;
foreach (SettingElement clientSetting in clientSettingSect.Settings)
{
Debug.WriteLine(String.Format("{0}={1}", clientSetting.Name, clientSetting.Value.ValueXml.InnerText ));
}
}
}
}
Notice that I cast the object instance to a ClientSettingSection to retrieve the settings value (which is a SettingElement).
If you put this to work with the sample config you provided the result in the Debug Output Window pane should be:
ApplicationTitle=Accessing Config files
VersionNo=V 1.0
Here's the idea I had:
I want a small executable to have an app.config file with multiple sections that are situated under the sectionGroup "applicationSettings" (not "appSettings", I don't need to write to the file). Each section would have a name corresponding to a module that should be loaded if set.
Here's an example:
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Executable" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="FirstModule" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Executable>
<setting name="MyFirstSetting" serializeAs="String">
<value>My awesome feature setting</value>
</setting>
</Executable>
<FirstModule path="path to the modules assembly">
<setting name="ImportantSettingToTheModule" serializeAs="String">
<value>Some important string</value>
</setting>
</FirstModule>
</applicationSettings>
</configuration>
Now if I define the FirstModule section, I want my application to load its assembly. If the section is not defined, the module should not be loaded. This should be true for not only one module but a not yet defined number of them.
So I basically need to find out about the defined sections at runtime. How would I do that?
In addition I want this to become a portable executable (= it has to run on Mono as well) that is backwards compatible to .NET 2.0.
It might be interesting to have a look at the project on GitHub (currently at this commit).
Take a look at the ConfigurationManager.OpenExeConfiguration function to load in your configuration file.
Then on the System.Configuration.Configuration class that you'll get back from ConfigurationManager.OpenExeConfiguration you'll want to look at the SectionGroups property. That'll return a ConfigurationSectionGroupCollection in which you'll find the applicationSettings section.
In the ConfigurationSectionGroupCollection there will be a Sections property which contains the Executable and FirstModule ConfigurationSection objects.
var config = ConfigurationManager.OpenExeConfiguration(pathToExecutable);
var applicationSettingSectionGroup = config.SectionGroups["applicationSettings"];
var executableSection = applicationSettingSectionGroup.Sections["Executable"];
var firstModuleSection = applicationSettingSectionGroup.Sections["FirstModule"];
You will want to check for null after getting the ConfigurationSectionGroupCollection object or ConfigurationSection objects. If they are null they don't exist in the configuraiton file.
You can also get the sections by using ConfigurationManager.GetSection
var executableSection = (ClientSettingsSection)ConfigurationManager
.GetSection("applicationSettings/Executable");
var firstModuleSection = (ClientSettingsSection)ConfigurationManager
.GetSection("applicationSettings/FirstModule");
Again, if the objects are null they don't exist in the configuration file.
To get a list of all the section names and groups you could do:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// use the line below instead if you want to load an app.config for a
// different application other than the one the code is running in
// var config = ConfigurationManager.OpenExeConfiguration(pathToExecutable);
var names = new List<string>();
foreach (ConfigurationSectionGroup csg in config.SectionGroups)
names.AddRange(GetNames(csg));
foreach (ConfigurationSection cs in config.Sections)
names.Add(cs.SectionInformation.SectionName);
private static List<string> GetNames(ConfigurationSectionGroup configSectionGroup)
{
var names = new List<string>();
foreach (ConfigurationSectionGroup csg in configSectionGroup.SectionGroups)
names.AddRange(GetNames(csg));
foreach(ConfigurationSection cs in configSectionGroup.Sections)
names.Add(configSectionGroup.SectionGroupName + "/" + cs.SectionInformation.SectionName);
return names;
}
When are settings from app.config actually read by application?
Suppose I have a windows service and some app settings for it. In code I have a method where some setting is used. Method is being called in every iteration, not just once during all the time. If I change the setting value through the configuration file should I restart the service for it to be "refreshed" inside or will it be accepted the next time without any interaction from my side?
You need to call ConfigurationManager.RefreshSection method to get the latest values read directly from disk. Here's a simple way to test and provide answer to your question:
static void Main(string[] args)
{
while (true)
{
// There is no need to restart you application to get latest values.
// Calling this method forces the reading of the setting directly from the config.
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine(ConfigurationManager.AppSettings["myKey"]);
// Or if you're using the Settings class.
Properties.Settings.Default.Reload();
Console.WriteLine(Properties.Settings.Default.MyTestSetting);
// Sleep to have time to change the setting and verify.
Thread.Sleep(10000);
}
}
My app.config containing:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConsoleApplication2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings>
<add key="myKey" value="Original Value"/>
</appSettings>
<userSettings>
<ConsoleApplication2.Properties.Settings>
<setting name="MyTestSetting" serializeAs="String">
<value>Original Value</value>
</setting>
</ConsoleApplication2.Properties.Settings>
</userSettings>
</configuration>
After you start the application, open the app.config within the build folder, and change the value of the appSetting "myKey". You'll see the new value printed out to the console.
To answer the question, yes they are cached on the first time they are each read I think, and to force the read straight from the disk, you need to refresh the section.
Either when you load it up via the configuration manager (ConfigurationManager.GetSection("x/y");) or when you try to access the properties.
There is a slight grey area here because when you get the configuration out via the config manager:
var config = (MyConfigSection)ConfigurationManager.GetSection("MyConfigSection");
You get a configuration object back if you have provided the configuration section type in the configurationSections element at the top of the config file. If you do not actually provide the actual config you will still get an object back.
However if you have a required field that is not set it will not throw an exception till you call the property. I have worked this out whilst trying to unit test my custom configuration sections.
I've stored configuration of my application in the app.config, by Visual Studio I've created some application key on the settings tab of project properties dialog, then I've set this key at application level(NOT at user level).
Visual Studio automatically generate the following xml file (app.config) :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AleTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<AleTest.Properties.Settings>
<setting name="DatabasePath" serializeAs="String">
<value>Test.s3db</value>
</setting>
<setting name="DatabaseUser" serializeAs="String">
<value />
</setting>
<setting name="DatabasePass" serializeAs="String">
<value />
</setting>
</AleTest.Properties.Settings>
</applicationSettings>
</configuration>
Now I want to save and reload the settings at runtime, here's my code that allow to save the value DatabasePath in the configuration file:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["AleTest.Properties.Settings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;
//Database Configuration Setting
SettingElement applicationSetting = clientSection.Settings.Get("DatabasePath");
applicationSetting.Value.ValueXml.InnerXml = this.textBoxPath.Text.Trim();
applicationConfigSection.SectionInformation.ForceSave = true;
config.Save();
The problem is that with this code the new settings aren't loaded by application until I restart the application; is there a way to reload the config settings at runtime?
I also want to replace the fixed value of the name of applicationSettings section (AleTest.Properties.Settings) with a variable value, exist a variable in the framework the assume this value (AleTest.Properties.Settings) ?
You need to make a call to ConfigurationManager.RefreshSection in order to have the values re-read from disk.
I did a some tests and here is result.
For auto generated class Settings the call of ConfigurationManager.RefreshSection("applicationSettings"); doesn't apply the modified values for members marked with ApplicationScopedSettingAttribute, it applies the changes to future calls via ConfigurationManager members (and not sure about UserScopedSettingAttribute).
Instead call Settings.Default.Reload();
What you want is accomplish able by creating an custom ConfigSection which allows you more control and allows you to change the name. Configuration manager has a refresh section which will allow you reload the data.
Aleroot's code for updating values worked fine.
In spite of Properties.Settings being write only (no set).
To refresh, this worked for me:
ConfigurationManager.RefreshSection("applicationSettings");
But I'm using this to access the parameter:
string test = Properties.Settings.Default.MyString;
MessageBox.Show("Paramètres/Settings MyString = " + test);