Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hi all,
 
How to write string array into app config(c#) file and how to read those array values into our .cs file.
 
Thanks in advance...
Posted 19-Mar-12 21:04pm
Comments
Dinesh Mani at 20-Mar-12 3:30am
   
Why do you want to write data into your app config???
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

probably the easiest way to do this is to enter the value of the key with a delimeter (eg a comma). Use the string's Split function to split the values and there you have your array. Depending on the type of your needed array, you'll need to convert.
 
config file
<add key="myarray" value="1,5,6,8,9" />
 
Code behind
string myarrayvalue = ConfigurationManager.AppSettings["myarray"];
string [] str_array = myarrayvalue.Split(new string[]{","}, StringSplitOptions.RemoveEmptyValues);
 
//if needed as eg. an integer.
int [] myintarray = new int[str_array.Length];
for(int i = 0; i < str_array.Length; i++){
  int output;
  if(int.TryParse(str_array[i], out output){
    myintarray[i] = output;
  }
  else{
    myintarray[i] = -1;
  }
}
 
This is a pretty basic question, please start out with a good book.
  Permalink  
Comments
Oshtri Deka at 20-Mar-12 4:02am
   
Old school ;).
5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

There is an existing class that can do just that: CommaDelimitedStringCollectionConverter Class[^]
 
There is an example as part of the ConfigurationConverterClass description[^]
  Permalink  
Comments
Oshtri Deka at 20-Mar-12 4:03am
   
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 20 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid