Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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
Comments
Dinesh Mani 20-Mar-12 3:30am    
Why do you want to write data into your app config???

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
C#
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.
 
Share this answer
 
Comments
Oshtri Deka 20-Mar-12 4:02am    
Old school ;).
5.
IzetFazlinovic 31-Aug-14 20:37pm    
"This is pretty basic question...", but question was "How to write string array into app config(c#) file...". It seems this part of question is not pretty basic for you - no answer...
There is an existing class that can do just that: CommaDelimitedStringCollectionConverter Class[^]

There is an example as part of the ConfigurationConverterClass description[^]
 
Share this answer
 
Comments
Oshtri Deka 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900