How to read a personal section in a web.config ?
<MyPersonalSection>
<add name="toto" enable="true" URL="http://localhost:43242" />
<add name="titi" enable="false" URL="http://localhost:98762" />
<MyPersonalSection/>
I'd like to get the enable value and/or URL value with the name value.
I also have this mistake : Unrecognized configuration section MyPersonalSection
I been trying
var config = ConfigurationManager.GetSection("MyPersonalSection");
Here is a cool example for that.
You don't need to write a custom configuration handler to get what you want. There are built-in configuration handlers that you can use if you simply want key-value entries. But, you'll have to use key instead of name and value instead of URL. For example:
<configuration>
<configSections>
<section name="MyPersonalSection" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyPersonalSection>
<add key="toto" value="http://localhost:43242" />
<add key="titi" value="http://localhost:98762" />
</MyPersonalSection>
</configuration>
And you can access them via code:
var myValues = ConfigurationSettings.GetConfig("MyPersonalSection") as NameValueCollection;
var url = myValues["toto"];
I would suggest naming your keys in a way that makes it clear what the value should be, like "totoUrl" and "titiUrl".
If you want something other than string value pairs, you'll have to write your own custom handler.
You can add appSettings section in your web.config with key that you will need. For example:
<configuration>
<appSettings>
<add key="FirstUrl" value="http://localhost:43242"/>
<add key="SecondUrl" value="http://localhost:98762" />
</appSettings>
...
</configuration>
So, since aspx.cs file, you can declare directive
using System.Configuration;
And later, you can retrieve FirstUrl value in this way:
var myUrl = ConfigurationManager.AppSettings["FirstUrl"];
Related
I'm trying to build an array of strings from items in a web.config file (in IIS).
web.config
<appSettings>
<add key="favourite" value="url1; site1" />
<add key="favourite" value="url2; site2" />
<add key="favourite" value="url3; site3" />
</appSettings>
C#
// Reads first favourite into string.
public string _favourites = ConfigurationManager.AppSettings ["favourite"];
I would like each favourite to be read into string [ ] _favourites array with the semi-colon (I would parse that out later). web.config is an XML file so I can open it as one and pull the data out, but is there an easier way to do this using ConfigurationManager?
What if you add all Array values in single key like -
<appSettings>
<add key="favourite" value="url1;site1,url2;site2,url3;site3" />
</appSettings>
Read that key value as a string -
public string _favourites = ConfigurationManager.AppSettings["favourite"];
and then split the string by ','(comma) like this -
string[ ] _favouritesArr = _favourites.Split(',');
This will give all values in array _favouritesArr.
I don't know if there is a hack to do it, but I would just have one setting with multiple values;
<appSettings>
<add key="favourite" value="url1;site1;url2;site2;url3;site3;" />
</appSettings>
or
<appSettings>
<add key="favourite" value="url1=site1;url2=site2;url3=site3;" />
</appSettings>
Another solution is a separate configuration file or store this in a database.
I imagine that the favourites would change and changing web.config has consequences - it may cause your app to restart.
How about:
1.right click on project > properties
2.Navigate to settings. If it shows, This project does not have settings file. click to create one.
3.Create a SystemCollections.Speciliazed.StringCollection
4.Name it, as you want (my example StatusReason). and in end of line on e right there is ...(three) dots
5.Press them and add as much settings needed. One per line.
Sample:
Valid=979580000
Invalid=979580001
Broken=979580002
ReadyForCollect=979580003
Missing=979580004
Refine=979580005
and in web config it looks like that.
<applicationSettings>
<WebApplication.BookServices>
<setting name="StatusReason" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Valid=979580000</string>
<string>Invalid=979580001</string>
<string>Broken=979580002</string>
<string>ReadyForCollect=979580003</string>
<string>Missing=979580004</string>
<string>Refine=979580005</string>
</ArrayOfString>
</value>
</setting>
</WebApplication.BookServices>
</applicationSettings>
and get your array of strings :
var settings = Settings.Default.StatusReason;
//TODO add any logic, if needed to split
add your project reference to properties:
using WebApplication.BookServices.Properties;
and there you have a array of settings.
I hope you can help me.
I'm supposed to add a new type of values to an AppSettings file (already existing with some values). Those values are a whole list of special folders so I thought the best way would be to have a new section for those folder values so that the file would look like this:for
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="usPath" value="folderName1" />
<add key="tcPath" value="folderName2" />
<add key="usGUID" value="folderID1" />
<add key="tcGUID" value="folderID2" />
</appSettings>
<updateFolders>
<add key="folderID3" value="folderName3">
<add key="folderID4" value="folderName4">
</updateFolders>
</configuration>
Reading and writing within the already existing appSettings-tag is no problem but I haven't find a way yet to modify the updateFolders section. I'm really new to using AppSettings in this way so I don't know too much about what's possible and what's not. In addition to that I think the AppSettings file might have been set up in a wrong way from the very beginning (it gets created by using a System.IO.File-Writer).
see ConfigurationManager.GetSection
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.getsection(v=vs.110).aspx
I would like to make an app.config that looks like
<configuration>
<SQLconneciton>
<add key=name/>
<add key= otherStuff/>
</SQLconnection>
<PacConnection>
<add key=name/>
<add key= otherStuff/>
</PacConnection>
</configuration>
I've read many examples where people make ONE custom section and add stuff, I need to allow the user to add multiple sections, read, delete. I don't really need fancy elements, just simple add and key values. Is section groups worth using or is there something easy I'm missing?
Sure - there's really nothing stopping you from creating as many custom config sections as you liked!
Try something like this:
<?xml version="1.0"?>
<configuration>
<!-- define the config sections (and possibly section groups) you want in your config file -->
<configSections>
<section name="SqlConnection" type="System.Configuration.NameValueSectionHandler"/>
<section name="PacConnection" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<!-- "implement" those config sections as defined above -->
<SqlConnection>
<add key="abc" value="123" />
</SqlConnection>
<PacConnection>
<add key="abc" value="234" />
</PacConnection>
</configuration>
The System.Configuration.NameValueSectionHandler is the default type to use for a config section that contains <add key="...." value="....." /> entries (like <appSettings>).
To get the values, just use something like this:
NameValueCollection sqlConnConfig = ConfigurationManager.GetSection("SqlConnection") as NameValueCollection;
string valueForAbc = sqlConnConfig["abc"];
And you can absolutely mix and match existing section handler types as defined by .NET as well as your own custom config sections, if you've defined some of those yourself - just use whatever you need!
Here is my app.config
<configuration>
<configSections>
<section name="procedureList" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.30319, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<procedureList>
<add key="NAS.spBusObjGetLineProd" value="#area='Melt Shop';#endDt=?date?;#dayonly=1;#obj='Melt Shop Business Objective" />
<add key="NAS.spBusObjGetLineProd" value="#area='Cold Mill';#endDt=?date?;#dayonly=1;#obj='Cold Mill Business Objective" />
</procedureList>
<appSettings>
<add key="Connstr" value=""/>
<add key="Userid" value=""/>
<add key="Timeout" value=""/>
</appSettings>
</configuration>
But when I call it in code, I'm getting a null back
public void samplemethod()
{
NameValueCollection nvc = ConfigurationManager.GetSection("procedureList") as NameValueCollection;
string[] keys = nvc.AllKeys;
}
I would appreciate any help pointing out what I've done wrong
Using section handlers to group settings in the configuration file
For example you can follow something like the following
private void ReadSettings()
{
NameValueCollection loc =
(NameValueCollection )ConfigurationSettings.GetConfig("procedureList");
}
MSDN ConfigurationManager.GetConfig Method
If you are testing your class you must copy the configuration to the app.config in your Test project.
using immediate window check which config file it is pointing to. in my case i had app.config which i am expecting it to read, but on using the command. ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) it is pointing to something else like nuintrunner.exe.config as that info is loaded into bin. this help in loading the right configuration file
Inside my web.config file I've got code like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
...
<section name="UninstallSurveySettings" type="dashboard.UninstallSurveyConfig" />
</configSections>
...
<UninstallSurveySettings>
<add key="fileLocation" value="C:\inetpub\wwwroot\output\" />
</UninstallSurveySettings>
...
</configuration>
I need to be able to access this field from my custom control. The control can be dropped into any website and needs to check that site's web.config for the fileLocation value in UninstallSurveySetting.
I've tried a couple different approaches with no luck. Any help on this would be greatly appreciated.
Much easier to use AppSettings.
Web.config:
<configuration>
<appSettings>
<add key="fileLocation" value="C:\inetpub\wwwroot\output\" />
</appSettings>
</configuration>
Code:
string location = System.Configuration.ConfigurationManager.AppSettings["fileLocation"];
If your section will become more complex, then:
var section = (NameValueFileSectionHandler)ConfigurationManager.GetSection("UninstallSurveySettings");
if (section != null)
{
// access section members
}
P.S.
Maybe you want to use ConfigurationSection class instead of handler.
In ASP.NET MVC 3 the tag cannot be a direct child of (it results in a configuration error).
How about adding your key to the section. Then you can easily access it via the ConfigurationManager.AppSettings collection.
using System.Configuration.ConfigurationManager and you will be able to get what you want from the web.config
I was able to solve this by creating a configuration class for it and placing this code in the web.config:
<section name="UninstallSurveyConfig" type="dashboard.UninstallSurveyConfig" />
..
<UninstallSurveyConfig dirFileLocation="C:\inetpub\wwwroot\build\output" webFileLocation="~/output" />