C# getting value from app.config - c#

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...

Related

Reading a string from app.config file

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

Dynamically encrypting user configuration settings file

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.

Access section 'applicationSettings' (not 'appSettings') in config file from setup

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

Set Queue Path in Web.config

I am using MSMQ in C#, how can i set queuepath in web.config?
Any suggestions?
Thanks
Add an appSetting key with the path of your Queque ?
<appSettings>
<add key="myquequepath" value="FormatName:DIRECT=OS:machinename\private$\MyQueue" />
</appSettings>
It was really simple i found solution.
<add key="MSMQName" value=".\private$\WebSiteEmails"/>

How to read name-value attributes from an app.config file?

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.

Categories

Resources