I am able to read the key-value attributes from the app.config file by using the following syntax.
System.Configuration.ConfigurationManager.AppSettings["ConfigurationFile"]
I want to read the name-value attributes from the app.config file which are defined under the following schema.
<applicationsettings>
<Host.Properties.Settings>
<setting name="Path" serializeAs="String">
<value>F:\PATH\</value>
</setting>
<Host.Properties.Settings>
</applicationSettings>
</configuration>
What is the correct solution?
System.Configuration.ConfigurationManager.AppSettings["Path"]
or this:
Host.Properties.Settings.Default.Path
should work.
Related
I've got the following example content generated by the Settings-Designer in Visual Studio 2015:
<configuration>
<applicationSettings>
<Test.Properties.Settings>
<setting name="LogFileFolder" serializeAs="String">
<value>logs</value>
</setting>
<setting name="sPluginsFolder" serializeAs="String">
<value>plugins</value>
</setting>
<setting name="sLangBaseName" serializeAs="String">
<value>Test.Resources.Language.Test</value>
</setting>
<setting name="ConfirmationExit" serializeAs="String">
<value>True</value>
</setting>
</Test.Properties.Settings>
</applicationSettings>
</configuration>
Now I want to access, for example, the variable "ConfirmationExit" within my code. In first I tried with Properties.Settings.Default which is working nicely, but I want to retrieve the configuration keys more generic and provide a ConfigurationService which can be used by any other .NET program.
I tried playing around with the ConfigurationManager, but I didn't get the values of the different settings/properties. I always got null if I tried something like this:
string test = ConfigurationManager.AppSettings["ConfirmationExit"];
How can I PROGRAMMATICALLY read AND write properties/settings defined within the app.config file with the use of the Settings-Designer. Thx for any help ;)
UPDATE:
Maybe I have to mention, that there is a namspace used as XML element (I think this is generated by the designer or soemthing else?!):
<Test.Properties.Settings>
inside of
<applicationSettings>
Also please remind:
This is a project structure like this:
1. Project_Common
2. Project_Main
|-- Project_Common
3. Project_Plugin1
|-- Project_Common
4. Project_Plugin2
|-- Project_Common
...
In Project_Common there should be a ConfigurationService which can retrieve all properties of all applications. Maybe the constructor has to have a parameter to inject settings or something.
string test = Properties.Settings.Default.ConfirmationExit;
From MSDN: https://msdn.microsoft.com/en-us/library/a65txexh.aspx
read:
Settings.Default.{propertyName}
so in your case for example
Settings.Default.ConfirmationExit
reads ConfirmationExit-Value
Set it:
Settings.Default.ConfirmationExit = false
Write:
Settings.Default.Save();
I have an application that connects to a database. Is there a way to encrypt the file that contains the connection string along with other settings? The file is quite exposed as the database connection password is stored inside the user.config file. The only semi-solution I found was for web applications by adding a web.config file, but I'm not sure if this applies the same way. Is there any documentation regarding configuration encryption?
Here's my attempt so far:
user.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<TimeClock.Properties.Settings>
<setting name="webserver" serializeAs="String">
<value>test.js.com/</value>
</setting>
<setting name="timeclockConnectionString" serializeAs="String">
<value>port=3306;server=localhost;user id=root;password=123456;database=timeclock;convertzerodatetime=True</value>
</setting>
<setting name="screenmode" serializeAs="String">
<value>0</value>
</setting>
</TimeClock.Properties.Settings>
</userSettings>
</configuration>
Method:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal);
string provider = "RsaProtectedConfigurationProvider";
ConfigurationSection connStrings = config.GetSection("timeclockConnectionString");
connStrings.SectionInformation.ProtectSection(provider);
connStrings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
As you can see, I'm trying to encrypt only the timeclockConnectionString and nothing else. Obviously, I get an error:
Object reference not set to an instance of an object.
which probably stems from the config.GetSection.
Any help would be greatly appreciated.
I'm trying to change a web reference path depending on whether the site is live or not.
The application settings are in the web.config
<applicationSettings>
<WebReferenceName.Properties.Settings>
<setting name="WebReferenceName_Service_TBService"
serializeAs="String">
<value>http://localhost:50711/Service.svc</value>
</setting>
</WebReferenceName.Properties.Settings>
I've tried the following:
Properties.Settings.Default.WebReferenceName_Service_TBService.Equals("http://www.newurl.com/service.svc");
which although doesn't error, checking it later, shows it hasn't changed.
and
var config = WebConfigurationManager.OpenWebConfiguration("~/web.config");
config.AppSettings["WebReferenceName_Service_TBService"] = "http://www.newurl2.com/service.svc";
config.Save();
but this errors saying I don't have permissions
Is there another way of doing this?
I'd rather not have to use if statements all over the place, as my types are namespaced differently.
e.g.
using (var service = new WebServiceLocal.TheWebServiceService())
{
WebServiceLocal.blah();
}
Thanks
The preferred way to do this is to use config transformations. So your default web.config would contain:
<applicationSettings>
<WebReferenceName.Properties.Settings>
<setting name="WebReferenceName_Service_TBService" serializeAs="String">
<value>http://localhost:50711/Service.svc</value>
</setting>
</WebReferenceName.Properties.Settings>
</applicationSettings>
And in your transform, you would have something like this (note the xdt:Transform="Replace"):
<applicationSettings>
<WebReferenceName.Properties.Settings>
<setting name="WebReferenceName_Service_TBService" serializeAs="String">
<value xdt:Transform="Replace">http://www.newurl2.com/service.svc</value>
</setting>
</WebReferenceName.Properties.Settings>
</applicationSettings>
A point to note, if you are doing something similar for an app.config (as opposed to web.config) you will need to install an addon like SlowCheetah.
I am in the process of creating a setup for a web application we build. No I have a configuration file, which looks something like like this, which contains a section 'appSettings', and a section 'applicationSettings':
<configuration>
<appSettings>
<add key="Password" value="dummy"/>
<add key="Username" value="dummy"/>
<add key="DB" value="dummy"/>
<add key="DBServer" value="dummy"/>
<add key="LogStoredProcedure" value="dummy"/>
<add key="ErrorStoredProcedure" value="dummy"/>
<add key="ErrorFileName" value="dummy"/>
<add key="EncryptionKey" value="dummy"/>
</appSettings>
<applicationSettings>
<inoBIBooks.My.MySettings>
<setting name="BIDB_Username" serializeAs="String">
<value>Username</value>
</setting>
<setting name="BIDB_Server" serializeAs="String">
<value>Servername</value>
</setting>
<setting name="BIDB_Database" serializeAs="String">
<value>Database</value>
</setting>
<setting name="BIDB_Password" serializeAs="String">
<value>Password</value>
</setting>
</inoBIBooks.My.MySettings>
</applicationSettings>
</configuration>
Now, from my setup I have to open the config file from the file system with
Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir);
where variable 'targetvdir' contains the path to the config file.
I get the config file by this, and I am able to edit the 'appSettings' section by
config.AppSettings.Settings["Password"].Value = "something";
But I am not able to do that anyway with the 'applicationSettings' section. In the web application itself I access that part by
Properties.Settings.Default.<Setting>
but that wont work from my setup project.
Is there a chance to edit the 'applicationSettings' section as easy as the 'appSettings' section? Or do I have to edit the xml itself?
Any hint is much appreciated.
Kind regards,
Kai Hartmann
I apologize for answering my question by myself, since I found the solution right after posting it. This question gave the answer basically: Save and reload app.config(applicationSettings) at runtime
I had to use this code, to write to that section 'applicationSettings':
// this gets the applicationSettings section (and the inner section 'inoBIBooks.My.MySettings')
Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir);
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["inoBIBooks.My.MySettings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;
// set a value to that specific property
SettingElement applicationSetting = clientSection.Settings.Get("BIDB_Username");
applicationSetting.Value.ValueXml.InnerText = "username";
// without this, saving won't work
applicationConfigSection.SectionInformation.ForceSave = true;
// save
config.Save();
I'm trying to open my project Settings XMl
in order to add some configurable pair to it
but I get this error:
Unable to load settings file. It might be corrupted or contain invalid XML or contain duplicate identifiers.
However, my XML is very thin:
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="Conduit.Mam.MaMDBEntityFramework.Properties" GeneratedClassAppName="Settings">
<Profiles />
<Settings>
<Setting AppName="SavedSuccessfullyInDB" Type="System.String" Scope="Application">
<Value Profile="(Default)">The Test saved in DB successfully</Value>
</Setting>
</Settings>
</SettingsFile>
I also had this error ("Unable to load settings file. It might be corrupted or contain invalid XML or contain duplicate identifiers.") which was also the result of resharper refactoring. After a little research, this was my solution:
Close Visual Studio
Navigate to the projects Properties directory
Delete Settings.settings and Settings.Designer.cs
Start Visual Studio and open your project
Add a new Settings (Properties -> Settings)
The beauty about this approach is that Visual Studio will read your app/web.config and notice that you already have entries and will automatically add those key/values for you.
The attribute for "SettingsFile" node should be "GeneratedClassName" not "GeneratedClassAppName". Likewise the attribute "AppName" on "Setting" node should be "Name".
In my case I think the cause was Resharper being too aggressive refactoring.
Your XML declaration is using single quotes. I thought it needed to be double quotes. Might be worth a try.
This simple case sensitivity issue in my app.config caused the issue for me
<setting Name="BlankPrescriberNoWhenWelshFormInGroup" serializeAs="String">
<value>2</value>
</setting>
this needed to be 'name' instead of 'Name'
<setting name="BlankPrescriberNoWhenWelshFormInGroup" serializeAs="String">
<value>2</value>
</setting>
However note I've also had this before when I typed a capital 'S' for
<Setting name="value" serializeAs="String">
in my app.config rather than a lower case 's' as in
<setting name="value" serializeAs="String">
so case sensitivity can get you here watch out! And another to watch out for and uppercase 'V' for
<Value>2</Value>
when it really only likes 'v' for
<value>2</value>