I have a WinForms application that I have changed the name of. I've changed the name everywhere I can find it, and can find no traces of the old name (except as below) and everything works fine; the one exception is in app.config, where if I change OLDNAME to NEWNAME any attempt to read any of the configuration settings throws a ConfigurationExceptioon of "Configuration system not ready", if I recall correctly.
Can anyone suggest how I can change the OLDNAME below to something else, such as my new name, without upsetting the config system?
<?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="OLDNAME.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<OLDNAME.Properties.Settings>
<setting name="MainWindowPosition" serializeAs="String">
<value>0, 0, 0, 0</value>
</setting>
<setting name="UseShortDeviceNames" serializeAs="String">
<value>False</value>
</setting>
</OLDNAME.Properties.Settings>
</userSettings>
</configuration>
The link http://www.codeproject.com/Articles/10981/Understanding-Section-Handlers-App-config-File will explain more using Section Handlers for config.
You must change alsow the section name, you section name is linked to the projects where is build and you have two posibility:
- rename the project (output) and in app.config
- create a new custom group and use the settings
Related
I have the following code structure:
xUnit tests
These use features from my.dll
The my.dll uses a third party logger.dll
If I use the my.dll outside of xUnit's testserver.dll, then I can just add a my.dll.config with the following contents:
<?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="Thirdparty.Logger.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<assemblySettings>
<add key="Hostname" value="127.0.0.1"/>
<add key="Port" value="1234"/>
</assemblySettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
<applicationSettings>
<Thirdparty.Logger.Properties.Settings>
<setting name="LogPath" serializeAs="String">
<value>C:\log\dir</value>
</setting>
</Thirdparty.Logger.Properties.Settings>
</applicationSettings>
</configuration>
And within my.dll's constructor, I can read them like follows, which works just fine.
AssemblySettings settings = new AssemblySettings();
var host = settings["Hostname"];
var port = settings["Port"];
Also the LogPath is passed on to logger.dll, which is closed source to me, but the logger.dll can definitely read the value passed on as LogPath.
Now, I have been successfully able to pass on the logger.dll's settings through xUnit as well using a testserver.dll.config file, which looks like follows:
<?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="Thirdparty.Logger.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
<section name="MyDll.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
<applicationSettings>
<Thirdparty.Logger.Properties.Settings>
<setting name="LogPath" serializeAs="String">
<value>C:\log\dir</value>
</setting>
</Thirdparty.Logger.Properties.Settings>
<MyDll.Properties.Settings>
<setting name="Hostname" serializeAs="String">
<value>127.0.0.1</value>
</setting>
<setting name="Port" serializeAs="String">
<value>1234</value>
</setting>
</MyDll.Properties.Settings>
</applicationSettings>
</configuration>
And also in this case, the LogPath is successfully passed on, but the settings for my.dll.config are not. The AssemblySettings instance is null:
AssemblySettings settings = new AssemblySettings();
// settings == null
What am I doing wrong?
Assembly.GetCallingAssembly().FullName in the constructor of AssemblySettings evaluates to "MyDll, Version=6.1.0.0, Culture=neutral, PublicKeyToken=null", but the returned instance is null.
I have a new (test) console application that I'm trying to access the settings.
When I attempt to access it this way, from what I've read on SO and other places, this should work:
var test1 = System.Configuration.ConfigurationManager.AppSettings["MySetting"];
but it doesn't, it returns null.
When I do it this way, it works fine:
var test2 = Properties.Settings.Default.MySetting;
Why doesn't the first one work? Everything I've read shows to use it the first way.
Am I using it incorrectly?
EDIT
In response to Mason.
I'm confused on your comment. The app.config is where the setting is being set from the properties of the application:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConsoleApp1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<applicationSettings>
<ConsoleApp1.Properties.Settings>
<setting name="MySetting" serializeAs="String">
<value>testtesttest</value>
</setting>
</ConsoleApp1.Properties.Settings>
</applicationSettings>
</configuration>
System.Configuration.ConfigurationManager.AppSettings is for reading settings from the app.config or web.config files (for ASP.NET). It does not read from the settings files.
Because AppSettings refers to reading the settings in MyApp.config
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've got this problem with my app.config file in my console application.
Here's my app.config
<?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="PROJECTNAME.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<PROJECTNAME.Properties.Settings>
<setting name="SETTING1" serializeAs="String">
<value>stringvariable</value>
</setting>
<setting name="SETTING2" serializeAs="String">
<value>boolvariable</value>
</setting>
<setting name="SETTING3" serializeAs="String">
<value>intvariable</value>
</setting>
</PROJECTNAME.Properties.Settings>
</userSettings>
</configuration>
If I only have the default schema (DotNetConfig.xsd) I get the following warnings:
Message 1 Could not find schema information for the element 'userSettings'. PROJECTPATH\App.config
Message 2 Could not find schema information for the element 'PROJECTNAME.Properties.Settings'. PROJECTPATH\App.config
Message 3 Could not find schema information for the element 'setting'. PROJECTPATH\App.config
Message 4 Could not find schema information for the attribute 'name'. PROJECTPATH\App.config
Message 5 Could not find schema information for the attribute 'serializeAs'. PROJECTPATH\App.config
Message 6 Could not find schema information for the element 'value'. PROJECTPATH\App.config
Message 3-6 repeats itself 3 times.
If I then go to XML > Create schema, it creates a app.xsd but then it gives me another warning:
Warning 1 The global element 'configuration' has already been declared. App.xsd
I've been trying to fix this problem for a few days now, with no result. And I can't find a solution using Google
I have a C# application where I store certain value in a Settings file like this:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="VITRIconEvacuationPlan.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="VITRIconEvacuationPlan.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<EvacuationPlan.Properties.Settings>
<setting name="AssemblyCentre" serializeAs="String">
<value>False</value>
</setting>
</EvacuationPlan.Properties.Settings>
</applicationSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><userSettings>
<EvacuationPlan.Properties.Settings>
<setting name="SymbolScale" serializeAs="String">
<value>25</value>
</setting>
</EvacuationPlan.Properties.Settings>
</userSettings>
</configuration>
Per default, the SymbolScale property is set to 25 (when I start the application the first time)
I want to change the SymbolScale property at runtime so I put this into User Scope. So I can say:
setting.SymbolScale = 150;
setting.save();
But when I close my application the value of the SymbolScale is 25 again. But I want it to store my chenged value from the runtime. What am I doing wrong?
I found a solution here:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ddeaca86-a093-4997-82c9-01bc0c630138/
I just had to change and save my SymbolValue like that:
Properties.Settings.Default.SymbolScale = 150;
Properties.Settings.Default.Save();
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.Save();
I am not sure why I have to call the save function two times but it works (And only with calling it two times)
Are you sure that you are checking the right "user.config" file?
At usual that stored in "C:\Users\xyzuser\AppData\Local\yourcompany\youarpp\version\user.config".
Hope that helps!
Set it like: Settings.Default.SymbolScale = 150;
Save it with Settings.Default.Save();