can't read variables out of app.config - c#

what I have in app.config is 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="Porject.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.0"/></startup>
<applicationSettings>
<Porject.Properties.Settings>
<setting name="PhotoLocation" serializeAs="String">
<value>.\photos\</value>
</setting>
</Porject.Properties.Settings>
</applicationSettings>
<connectionStrings>
<add name="****" connectionString="Data Source=***;Initial Catalog=****;User ID=***;Password=***" />
</connectionStrings>
</configuration>
and this is how I call the PhotoLocation:
string s = ConfigurationManager.AppSettings["PhotoLocation"];
When I make a request to get the connectionstring, there is no problem and it works fine; but when I requst the PhotoLocation it returns null.
Also the ConfigurationManager.AppSettings.Count returns 0.
Does anyone have an idea on what I'm doing wrong?

simply add this in your App config file
<appSettings>
<add key="PhotoLocation" value=".\photos\"/>
<appSettings>

ConfigurationManager.AppSettings reads (as the name might suggest) the AppSettings block of your configuration.
Given you've created your own section, you want to use var section = ConfigurationManager.GetSection("Porject.Properties.Settings") and read the values from that section.

Related

System.ArgumentException: 'Keyword not supported: 'metadata

What is this Error.
System.ArgumentException: 'Keyword not supported: 'metadata'.'
Line of follow is my app.config.
<?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="Foroush.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.5"/>
</startup>
<connectionStrings>
<add name="ForoushEntities" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Foroush;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<applicationSettings>
<Foroush.Properties.Settings>
<setting name="Foroush_SmsWebService_SendReceive" serializeAs="String">
<value>http://ip.sms.ir/ws/SendReceive.asmx</value>
</setting>
</Foroush.Properties.Settings>
</applicationSettings>
</configuration>
For Example
In the screenshot I see you alter the connection string by replacing an important part with "" in an attempt to create a Sql Connection String instead of an Entity Framework Connection String.
There are build-in methods for that like:
var entityConnectionStringBuilder = new EntityConnectionStringBuilder(MyConnectionString);
string sqlConnectionString = entityConnectionStringBuilder.ProviderConnectionString;
return new System.Data.SqlClient.SqlConnectionStringBuilder(sqlConnectionString).UserId);
In the screenshot code you probably wanted to use ConnectionStringNewFormat as an argument in the SqlConnectionStringBuilder instead of MyConnectionString (which is the original Entity Framework Connection String. But please, do not use string manipulation but use the code provided to you by the Entity Framework as demonstrated above.

configuration system failed to initialize ==> unrecognized configuration section

I don't even know if I can do what I'm attempting but I've imported forms from several projects and added references to those projects. Each project has a different set of connection strings and I'm trying to get them to coexist in App.config where I can filter by SECTION (Users select connections from comboboxes). I am hoping I can do this by implementing ConfigSections. If it's doable I obviously don't know how.
Attached is my App.config. I'm getting the error 'configuration system failed to initialize' and when I drill into the detail it says 'unrecognized configuration section amSettings
Is what I'm trying to do possible? If so, what do I need to correct?
<?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="amSettings.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser"
requirePermission="false"/>
<section name="cbSettings.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</sectionGroup>
</configSections>
<amSettings>
<add key="VX130 Attribute Map Connections" value="Sample Console Application" />
<add key="Region 1 VX130" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 VX130" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 VX130" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 VX130" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="CDW" value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
</amSettings>
<cbSettings>
<add key="CDW Class Builder Connections" value="Sample Console Application" />
<add key="Region 1 Class Build" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 Class Build" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 Class Build" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 Class Build" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="DEVELOPMENT Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="PREVIEW Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="VERSION Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
</cbSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Change the section name from amSettings.Properties.Settings to amSettings and cbSettings.Properties.Settings to cbSettings
e.g.
`<section name="amSettings" `
Here is a comprehensive example:
If you change your config file to this:
<configSections>
<section name="amSettings"
type="System.Configuration.AppSettingsSection"
allowExeDefinition="MachineToLocalUser"
requirePermission="false"/>
<section name="cbSettings"
type="System.Configuration.AppSettingsSection"
requirePermission="false"/>
</configSections>
<amSettings>
<add key="ABC" value="DEF"/>
</amSettings>
Then you can access the key ABC using this code:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("amSettings");
var a = appSettingSection.Settings["ABC"].Value;
The solution was two things. Change section name as user469104 recommended and wrapping sections in Group Name.
<?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="amSettings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser"
requirePermission="false"/>
<section name="cbSettings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</sectionGroup>
</configSections>
<ApplicationSettings>
<amSettings>
<add key="VX130 Attribute Map Connections" value="Sample Console Application" />
<add key="Region 1 VX130" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 VX130" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 VX130" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 VX130" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="CDW" value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
</amSettings>
<cbSettings>
<add key="CDW Class Builder Connections" value="Sample Console Application" />
<add key="Region 1 Class Build" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 Class Build" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 Class Build" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 Class Build" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="DEVELOPMENT Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="PREVIEW Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="VERSION Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
</cbSettings>
</ApplicationSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

"Configuration system failed to initialize" error

I'm getting this exception when using the code:
var appSettings = ConfigurationManager.AppSettings; //Exception here
string result = appSettings["ILFSsqlServer"] ?? "Not Found"; //trying to get to this point
I'm not really sure why and from previous answers to this question I think I'm doing exactly what they suggest but to no avail.
My app.config is:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</configSections>
<AppSettings>
<add key="ILFSsqlServer" value="ODBC;DSN=sql server copycloas;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=ILFSView;"/>
</AppSettings>
</configuration>
On your app.config you should write appSettings, lowercase A.

Storing settings in configFile

Try to save all data in to config file, and then read it and apply to my program during running - as user preference.
Whats done: create new config file (using Add new element-> add congif file). In this file put simple code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSetting>
<add Key="Volume" value="100" />
</appSetting>
</configuration>
After it create method
public int GetVolumeFromConfigFile()
{
return Convert.ToInt32(ConfigurationManager.AppSettings.Get("Volume"));
}
and in main programm call it like
Value = (MyClass.GetVolumeFromConfigFile());
But it's not work. (During debaggin it's return nothing)
Think it can be few reason:
I need to add (I don't now in what way) what config file to use, because i have few files *.config - one as default (App.exe.config, and another - that i created)
I use incorrect method to get value from config file
Also I read about some another way to store app settings, like store it in *.settings file
What I'm doing wrong and what method prefered?
Additional information - use net 4.0
EDIT
Remove my config file, and add to existed few lines (in strong>)
<?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="PlayDemo.SettingsPlayIt" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<userSettings>
<PlayDemo.SettingsPlayIt>
<setting name="Volume" serializeAs="String">
<value>10</value>
</setting>
</PlayDemo.SettingsPlayIt>
</userSettings>
Here I add my key
<appSetting>
<add key="Volume" value="100" />
</appSetting>
</configuration>
try this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Volume" value="100" />
</appSettings>
</configuration>
and
return Convert.ToInt32(ConfigurationManager.AppSettings["Volume"]);
the appSettings are key value pairs, so you can access it like you would a value in a Dictionary
If you want to use a separate config file, try this:
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.AppSettings.File = "yourFileName"'tell Configuration what file to read
config.Save(ConfigurationSaveMode.Modified) ' save the Configuration setting
ConfigurationManager.RefreshSection("appSettings") ' update just the <appSettings> node
I really like the following technique, using ConfigurationSection. This allows you for painless manipulation of your configuration. But it is more specific upfront.
http://msdn.microsoft.com/en-us/library/2tw134k3%28v=vs.90%29.aspx

Multiple AppSettings files, is it possible?

I want to create 3 AppSettings config files:
Database.config
Messages.config
Global.config
And after add in my App.config:
<appSettings file="Database.config" />
<appSettings file="Messages.config" />
<appSettings file="Global.config" />
But when I try to access a key that there is in one of three files with the ConfigurationManager, I got the following error:
Configuration system failed to initialize. Sections must only appear once per config file.
I cannot have more than one AppSettings config file?
You can't have more than one appsettings because that's the name of a section. You can add a new section though that uses the same kind of section definition as appsettings. E.g.,
<configuration>
<configSections>
<section name="DatabaseConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
....
<DatabaseConfig>
<add key="Whatever" value="stuff"/>
</DatabaseConfig>
</configuration>
Code for separate file:
Web.config:
<configSections>
<section name="DatabaseConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="MessageConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="GlobalConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<DatabaseConfig configSource="database.config">
</DatabaseConfig>
<MessageConfig configSource="message.config">
</MessageConfig>
<GlobalConfig configSource="global.config">
</GlobalConfig>
database.config:
<DatabaseConfig>
<add key="Name" value="ServerName" />
</DatabaseConfig>
etc...
Can be accessed via code like this:
var databaseConfiguration = (NameValueCollection)ConfigurationManager.GetSection("DatabaseConfig");
string name = databaseConfiguration["Name"];

Categories

Resources