Loop through multiple appSettings inside config file

时间:2016-05-03 20:10:11

标签: c# app-config

My requirement is to have multiple jobs and I have to loop through the JobName's of all jobs to check whether it exists currently.

    <configuration>
      <Job>
       <appSettings>
        <add key="JobName" value="BigDataUpload" />
       </appSettings>
      </Job>
      <Job>
       <appSettings>
        <add key="JobName" value="QueryUpload" />
       </appSettings>
      </Job>
    </configuration>

I am not sure whether this facility is present in C#. Currently I have one AppSettings inside Configuration and I access it using ConfigurationManager.AppSettings.Get("JobName").

Any help is appreciated !!

1 个答案:

答案 0 :(得分:0)

我不能做到这一点,但你可以使用:

<configuration>
    <appSettings>
        <add key="JobName1" value="Job1" />
        <add key="JobName2" value="Job2" />
        ...
        <add key="JobName(n)" value="Job3" />
    </appSettings>
</configuration> 

然后你可以使用一个循环加载它们直到它们为空:

int jobNumber = 1;
string lastJob = null;
do
{
    jobNumber++;
    lastJob = ConfigurationManager.AppSettings["JobName" + jobNumber];

    if(lastJob != null)
        //process the job name

}while(lastJob != null);