0

I want to add multiple int values to my Settings. So far I have this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1" 
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers"   
requirePermission="false"/>
</configSections>

<DigiPortServer1>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</DigiPortServer1>
</configuration>

Is this right? I have found many questions considerung multiple String values. How can I access these values? I would like to save these into a int array or something similar.

plm36021
  • 55
  • 9

2 Answers2

0

You could access the configuration section value using the ConfigurationManager GetSection method.

var section = System.Configuration.ConfigurationManager.GetSection("DigiPortServer1") as System.Collections.Specialized.NameValueCollection;
        var value = section["keyname"];

If section is the name value pair then cast with above type (NameValueCollection) or you can use own type to cast.

Balaji Marimuthu
  • 1,940
  • 13
  • 13
0

I would modify your file to be in this way. So it will be easier to access and to work with. This way you can easily access those features by writing ConfigurationManager.AppSettings["3"].ToString(); Note that your file wasn´t having the keys associated with any values. I added the value atribute.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1"    
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers" 
requirePermission="false"/>
</configSections>

<appSettings>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</appSettings>
</configuration>
NicoRiff
  • 4,803
  • 3
  • 25
  • 54
  • Thanks! This looks good, although I would like to use these values as int values. Any other solution than parsing it from string to int? – plm36021 Jan 09 '17 at 13:54
  • Simple, instead of casting as string, cast as int Convert.ToInt32(ConfigurationManager.AppSettings["3"]) – NicoRiff Jan 09 '17 at 13:55
  • you can cast as the data type you need – NicoRiff Jan 09 '17 at 13:55
  • Actually your suggestion does not create any error, but as a console output I only get 0. Can you spot any mistake? int x = Convert.ToInt32(ConfigurationManager.AppSettings["3"]); Console.WriteLine(x); int z = Convert.ToInt32(ConfigurationManager.AppSettings["4"]); Console.WriteLine(z); – plm36021 Jan 09 '17 at 14:01
  • It should not. It´s appSettings basics. You sure you are accesing the file correctly? – NicoRiff Jan 09 '17 at 14:05
  • Nope, I am not really sure now. The XML is in my App.config. – plm36021 Jan 09 '17 at 14:06
  • look at how to work it: http://stackoverflow.com/questions/10766654/appsettings-get-value-from-config-file – NicoRiff Jan 09 '17 at 14:10
  • try the full reference: System.Configuration.ConfigurationManager.AppSettings["3"] – NicoRiff Jan 09 '17 at 14:11
  • Thanks! This helped a lot as this was a correct answer and it is working now. – plm36021 Jan 09 '17 at 14:15