In an effort to try to get ConfigurationManager.AppSettings[..] working again, I've created one applicationSettings element and one appSettings element in a single-project console application.
<applicationSettings>
<el_testo.Properties.Settings>
<setting name="ApplicationSetting" serializeAs="String">
<value>Application Value</value>
</setting>
</el_testo.Properties.Settings>
</applicationSettings>
<appSettings>
<add key="AppSetting" value="App Value"/>
</appSettings>
Here's my effort to retrieve the two values.
Debug.WriteLine("---> ApplicationSettings Test");
Debug.WriteLine(ConfigurationManager.AppSettings["ApplicationSetting"] + "");
var applicationSettingsTest = ConfigurationManager.AppSettings["ApplicationSetting"];
Debug.WriteLine(applicationSettingsTest);
Debug.WriteLine("---> AppSettings Test");
Debug.WriteLine(ConfigurationManager.AppSettings["AppSetting"]);
Debug.WriteLine("---> Complete");
And here are my results.
---> ApplicationSettings Test
---> AppSettings Test
App Value
---> Complete
I could just "settle" and use the appSettings construct. It would be kind of nice, however, to use the Settings tab on Project properties on this project and future projects. Here's what I've tried this far to make work.
There's only one project in this application. Therefore, I can't be setting the value in one project and trying to read it in another. (Additionally, appSettings works.)
I verified that the project includes the assembly reference System.Configuration and the class includes the proper namespace reference (using System.Configuration;)
I've double-checked spelling and case of the setting name in ConfigurationManager.AppSettings["ApplicationSetting"].
I've tried linking App.config to different XML Schema files, EnterpriseLibrary.Configuration.xsd and an XML Schema file I generated using the XML menu in Visual Studio.
I've wiped the contents of the Debug and Release folders and rebuilt the project.
You are incorrectly trying to read your ApplicationSetting element as a key under appSettings with the following statement:
ConfigurationManager.AppSettings["ApplicationSetting"]
This means your are trying to get the value of this:
<appSettings>
<add key="ApplicationSetting" value="xxxx" />
</appSetting>
Instead; when you add an application setting using the project properties, it creates a settings class extending the ApplicationSettingsBase class, create properties with the name of your settings and will also add necessary configSection and a default config element in your app.config file. Why not just create and use that class which will give you type strict access to your application settings?
Just go to Project/e_testo Properties/Settings and add ApplicationSetting with scope Application
You can then access the ApplicationSetting by
var applicationSettingsTest = Properties.Settings.Default.ApplicationSetting
Related
This has been asked before, but I can't find an answer that works for me.
I have a solution that contains 5 projects, one is a Web API (.net 4.5) and one is a Class Library (.net 4.5). The Web API has a reference to the class/DLL. In the class project, the Settings.settings is utilized to enter values in the App.Config. All the values in the settings are Type string, Scope Application, Access Modifier Internal.
These values are created in the App.Config and look something like this:
<applicationSettings>
<xxxxx.Properties.Settings>
<setting name="setting1" serializeAs="String">
<value>1</value>
</setting>
<setting name="setting2" serializeAs="String">
<value>abc123</value>
</setting>....
In the class code, the values are read like so:
Properties.Settings.Default.setting1;
When the package for the Web API is created, it has the .dll, but not the .dll.config file. If I manually copy the .dll.config file, the .dll doesn't read from it. It also doesn't seem to read the ConnectionString fromt he web.config either.
I need the DLL to read either the .dll.config or the Web APIs web.config file. I have tried several solutions posted in stackoverflow, but most seem updated/deprecated and/or throw errors when I tried them.
What is the current and correct approach to this issue, with .net 4.5?
Thanks
I ended up deleting everything from the Settings.settings eliminating the
<applicationSettings>
<xxxxx.Properties.Settings>...
section of the App.Config. Then I just added a regular <appSettings> section and calling the value in code with
ConfigurationManager.AppSettings["xxxxx"];
I read in some posts that this was no longer supported, but it was the method that worked for me.
I have inhertied an web application (.Net MVC) to support. I have noticed that the previous developer has created a class library (myApp.Core) and used a settings file (AppSettings.settings) to store application specific settings, I also can see an associated app.config file that has all the settings defined in:
<applicationSettings>
<myApp.Core.AppSettings>
<setting name="DaysToKeep" serializeAs="String">
<value>2</value>
</setting>
...
</myApp.Core.AppSettings>
</applicationSettings>
I undersatnd that using a settings file gives nice strongly typed values and they can be stored agasint a user or application and the values can be accessed in code using the following notation:
AppSettings.Default.DaysToKeep;
However recently a change was required to update one of these setting's values on a production server (to issue a quick fix) however I was unable to locate where the actual app.settings file was located on the server and the exact setting and value to change.
Locally I can see myApp.core.dll.config in myApp.Core bin folder.
So... Where is the app.config file located on the web server? Also reading numberous articles seems to suggest that these values can't be upadted anyway, is this true.
I have 2 projects in my solution.
1 is an MVC Web application
The other is an empty website with a Web Service
Yesterday I added a setting via right-click on the Web Service project and go to properties => add settings manually.
This creates a settings file and adds the settings to Web.Config.
today I manually modified the Web.config file (I'm not sure this is the cause) but for some reason the following code cannot get my setting out of my Web.Config:
string connstring = WebConfigurationManager.AppSettings["someString"];
It just returns null
The setting name is correct.
The MVC-projects also has settings added in the same manner, there I can get all settings without any problems.
Things I've tried:
Double/Triple/Quadruple checked if setting name is correct
I have already restarted visual studio as administrator.
I have removed the settings file and recreated it. => this automatically also modifies Web.Config
Checked if there is a reference to System.Configuration
Another strange thing that I notice is the following:
When I type "WebConfigurationManager" in the immediate window => it looks like there are 0 AppSettings in it.
It also indicates that I have 2 connectionstrings while they really are not there (no where to be found in the solution if I perform Ctrl+Shift+F):
This is the connectionstring "WebConfigurationManager.ConnectionStrings[0]" returns:
ConnectionString: "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
I would almost think it's opening the wrong Web.config (but not one in any of my projects since the connectionstring here above does not exist in my solution)
Thanks for any tips !!
Extra info, contents of web.config:
....
<applicationSettings>
<SomeWebServices.Properties.Settings>
<setting name="someString" serializeAs="String">
<value>DATA SOURCE=abcdefg;USER ID=ABC;PASSWORD=abc987</value>
</setting>
<setting name="someOtherString" serializeAs="String">
<value>DATA SOURCE=abcde;USER ID=ABC;PASSWORD=abc654</value>
</setting>
</SomeWebServices.Properties.Settings>
</applicationSettings>
Since you are using applicationSettings and not appSettings in you web.config you'll need to access your setting via Properties.Settings.Default.someString
If you want to use string connstring = WebConfigurationManager.AppSettings["someString"]; you'll have to add the setting in the appSettings part of your web.config.
Edit:
If what you really want is somewhere to put your connection strings you should follow BilginAksoy's answer and use the connectionStrings part of your web.config.
If you use .NET 3.5 or above, do not use appsettings in web.config. Instead use the connectionStrings section in web.config.
try this web.config
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
I'm trying to update a value in my app.config file using the code below (the value is defined in Properties > Settings as Application scoped)
System.Configuration.Configuration configApp = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MessageBox.Show(configApp.AppSettings.Settings.Count.ToString()); //this shows 0
configApp.AppSettings.Settings["PontajAdminPwd"].Value = "dsfs";
configApp.Save(ConfigurationSaveMode.Full);
but it saying that configApp.AppSettings.Settings is empty...
This is a part of my app.config file
<applicationSettings>
<PontajWPF.Properties.Settings>
<setting name="PontajAdminPwd" serializeAs="String">
<value>696W3oybVP85szuiY2Qpiw==</value>
</setting>
</PontajWPF.Properties.Settings>
</applicationSettings>
What am I doing wrong?
Thank you
EDIT 1: I'm in a hurry so I adopted the solution proposed here (direct file access after changing the app.config file by hand - using appSettings instead of applicationSettings):
http://www.longhorncorner.com/uploadfile/rahul4_saxena/update-app-config-key-value-at-run-time-in-wpf/
configApp.AppSettings.Settings.Count.ToString() this will try to read settings from <appSettings> section, not <applicationSettings>. Also the name of the file should app.config.
In your case you will need to use Properties.Settings static class, to access your settings from applicationSettings. You can try PontajWPF.Properties.Settings.Default.PontajAdminPwd
Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions.
User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method. These settings are saved in the User.config file.
Read more on MSDN
Hope this helps.
I am using ConfigurationManager.AppSettings[myKey] to read a value from the app.config file in my windows application, but the value returned is always null, even though the key exists and it has a value, Deas any one know why?
Thanks
One, perhaps easier, alternative is to use a Settings file. This encapsulates the creation and maintenance of App.config values in a designer GUI and generates code for accessing the values.
To add a Settings file, right click your project in VS and click 'Add -> New Item', select 'Settings file' and give it a meaningful name, e.g. MainSettings.settings. You can then add an item, e.g. Foo, specify whether it is application or user-wide, define it's type and a assign it a value. In your code you can retreive the value by simple writing MainSettings.Default.Foo.
After compilation, you can change the value by editing the config file. The setting will appear as follows:-
<applicationSettings>
<YourNamespace.MainSettings>
<setting name="Foo" serializeAs="String">
<value>Bar</value>
</setting>
</YourNamespace.MainSettings>
</applicationSettings>
Hard to say from what you've provided here:
Check your spelling of the value in myKey
Ensure you are looking at the right app.config - if this call is in a referenced library and you're expecting a value to come from the calling project's app.config, but your library has an app.config for some reason it may be causing your problem.
i have two project in my solution first i add app.config file in class library project which all instances is call from console application i added these entries in config file in class lib project
<appSettings>
<add key="userName" value="user2" />
<add key="emilsLimit" value="50" />
</appSettings>
it was throwing null exception when i get these in a class in class library project but when i delete app.config from class Library project and added in Console project it works.Cheers
Note: Class lib project reference is added in console
I was having the same problem, but when I added an empty string ( + "") on the end it picks up the string in the appsettings
for example
string s = ConfigurationManager.AppSettings["myKey"] + "";