My question isn't related to the connection string which is the most popular. The other only help I could find with this is someone not using the reference.
What I'm trying to do: I have a string stored in the app.config file, which looks like:
<applicationSettings>
<App_5.Properties.Settings>
<setting name="Location_Name" serializeAs="String">
<value>String I want</value>
</setting>
My code to pull the string is:
string Loc_Name = ConfigurationManager.AppSettings["Location_Name"];
I have the reference added, and used. When I run the program my return is always null.
You are storing it in the wrong place in the app.config. For single name/value pairs use the appSettings. Then your retrieval code in the question will work.
<configuration>
<appSettings>
<add key="Location_Name" value="String I want" />
</appSettings>
</configuration>
Code (repeated from question)
var value = ConfigurationManager.AppSettings["Location_Name"];
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.
Is it possible to read the setting value URL using ConfigurationManager ?
I'm trying to read this section, but all I get it's null values. I could parse it using XmlDocument... I have already looked for this question within this forum but all I get is ways to get connectionstring, which is quite easy since ConfigurationManager has a ConnectionString Method.
<applicationSettings>
<ProjectoGlobal.Properties.Settings>
<setting name="ProjectoGlobal_WebServicePg_SyncPgData" serializeAs="String">
<value>http://localhost/SarfWebService/SyncPgData.asmx</value>
</setting>
</ProjectoGlobal.Properties.Settings>
</applicationSettings>
Thanks
Well, you can access it using:
Properties.Settings.Default.ProjectoGlobal_WebServicePg_SyncPgData
Here's MSDN reference: http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx
Thanks for all -1's ;)
It is very simple:
<configuration>
<appSettings>
<add key="myHttpString" value="http://localhost/SarfWebService/SyncPgData.asmx" />
</appSettings>
</configuration>
Documentation: MSDN
I think this would do:
string setting = yourprojectname.Properties.Settings.Default.ProjectoGlobal_WebServicePg_SyncPgData;
EDIT:
ok using the configuration manager just for demonstration purposes:
Configuration conf;
this.conf = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
ConfigurationSectionGroup sectionGroup = this.conf.GetSectionGroup("applicationSettings");
ClientSettingsSection section =
(ClientSettingsSection)sectionGroup.Sections.Get("ProjectoGlobal.Properties.Settings");
SettingElement setting = section.Settings.Get("ProjectoGlobal_WebServicePg_SyncPgData");
string result = setting.Value.ValueXml.InnerText;
you should use the first option though...
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 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.