Reputation: 7672
I have a connection string stored within a .config file which I don't know how to read from.
I've searched around and most I found is about how to read key/value pairs stored within AppSetting. But this file is organized differently. All I need is to get the value of ConnectionString.
NOTE: I cannot modify .config
file. It is given to me.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Assessment.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<Assessment.Properties.Settings>
<setting name="ConnectionString" serializeAs="String"> //This value I need
<value>Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[%CURRENT%]\DB.mdb</value>
</setting>
</Assessment.Properties.Settings>
</userSettings>
</configuration>
Upvotes: 5
Views: 18598
Reputation: 1035
connectionString = ConfigurationManager.AppSettings["ConnectionString"];
and in your config
<appSettings> <add key="ConnectionString" value="whatever" /> </appSettings>
Upvotes: 2
Reputation: 842
You can access `userSettings' section like:
var userSection = (ConfigurationSection)ConfigurationManager.GetSection("userSettings");
Upvotes: 0
Reputation: 146603
Without looking at the custom configuration section handler I can only guess:
Try this in your code:
var connString = System.Configuration.ConfigurationManager.GetSection
("Assessment.Properties.Settings")["ConnectionString"];
But better still is switch to use Microsoft Built-in ConnectionString section
System.Configuration.ConnectionStringSettings
Upvotes: 0
Reputation: 7961
Use the ConfigurationManager.ConnectionStrings property to retrieve connection strings from the application configuration file.
You should be storing your connection strings in the connectionStrings section of the configuration file.
Upvotes: 5
Reputation: 8455
There will be Settings class in the namespace of your project (Assessment.Properties.Settings)
The class is autogenerated.
To access your connection string simply use
Assessment.Properties.Settings.Default.ConnectionString
Upvotes: 6