Edit Appsettings in Winforms C# - c#

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");

Related

Referenced DLL, in Web API project not reading dll.config nor web.config

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.

App.config - Values in <applicationSettings> fail, Values in <appSettings> work

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

Why can I not find an AppSetting in my Web.Config with WebConfigurationManager?

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>

Updating application settings in WPF application

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.

Using config files in MSTest (testConfig)

I´m trying to use MyProject.config in MSTest. I added this file in "Deployment" (testConfig).
What is the best practice to access those fields? Can i build something like Java Properties?
Example:
<host>localhost</host>
<port>0</port>
<user>me</user>
Edit:
I have two Unit Test suite . I want to use diferents config files in each test.
Is the ConfigurationManager.AppSettings property of any use? Give that a try and see if it works, like this:
string port = System.Configuration.ConfigurationManager.AppSettings("port");
You'd need to refactor your configuration file to be a standard "App.config" file, and you add key/value pairs like this:
<configuration>
<appSettings>
<add key="port" value="80" />
</appSettings>
</configuration>

Categories

Resources