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.
Related
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
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 have created a user setting using visual studio 2010 as below
Client.exe.config
<userSettings>
<Client.Properties.Settings>
<setting name="ParamValue" serializeAs="String">
<value>OFF</value>
</setting>
</Client.Properties.Settings>
</userSettings>
However if i modify the value during runtime from "OFF" to "ON", the application still access the old value. But if i close the app and open again then it reads the updated value.
Is there someway to make the exe read the updated user settings at runtime .
The ApplicationSettingsBase class has a Reload method on it you can use to reload the settings from disk.
Properties.Settings.Default.Reload();
// Properties.Settings.Default.ParamValue will be reloaded from the file on disk.
You didn't specify how you were changing the settings, but it's worth noting that if you update 'User' settings through the 'Save' method of the settings instance, Reload will no longer reset the value if the saved value was different to the default. This is because 'User' settings are written to a settings file in the user profile folder and once they're saved you would have to update that version of the file, not the one in the bin folder for your application.
Specifically, 'User' settings that are saved back to disk through the Save method are written to the following location.
%USERPROFILE%\AppData\Local\<YourAppName>\<MangledName>\<Version>\user.config
If you want to reset your settings back to the default value you've defined in your App.config, use the Reset method.
Properties.Settings.Default.Reset();
// Properties.Settings.Default.ParamValue will be reset to the default.
One approach would be to read the settings into an object on load. Use that object as you want, updating the values if needed. Then on closing of the application write the values of the object back to the config file.
I need to change my Webreference url in C# windows app.
My app.config file has applicationSettings as
<applicationSettings>
<DataAggregator.Properties.Settings>
<setting name="DataAggregator_WebService_AccessDB" serializeAs="String">
<value>http://twks-126/Webservice/AccessDB.asmx</value>
</setting>
</DataAggregator.Properties.Settings>
</applicationSettings>
I need to change the value at runtime to new webservice.
When I try to get the configurationmanager.appsettings I don't get the settings.
Am I doing something wrong?
Thanks.
Try changing the app.config configuration to the following;
<appSettings>
<add key="DataAggregator_WebService_AccessDB" value="http://twks-126/Webservice/AccessDB.asmx"/>
</appSettings>
If you want to access the data pointed by <DataAggregator.Properties.Settings> you need to use this syntax in your code
string url = DataAggregator.Properties.Settings.Default.DataAggregator_WebService_AccessDB;
Keep in mind however that, if this settings has been configured as an Application Scope you will not be able to save changes back to the configuration file.
Your syntax could be used to access a different section of you config file. This section is called AppSettings and it's not the same as applicationSettings
var config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value.ToString());
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("appSettings");