-2

我有一个这样的 app.config 文件:-

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="botLogs" value="d:\TFS Projects-BotLogs/"/>
    <add key="botfolder" value="C:\BOTSCONFIG"/>
  </appSettings>
  <connectionStrings>
    <add connectionString="Server=1.1.1.1;Database=BGAppCrawling;UID=sa;Password=something;" name="VGDB"/>
    <add connectionString="Server=1.1.1.1;Database=BGMappingStaging;UID=sa;Password=something;" name="VGDBMapping"/>
  </connectionStrings>
</configuration>

现在我可以根据 appSettings 部分中的键获取值,如下所示:-

string filePath = ConfigurationSettings.AppSettings["botLogs"].ToString();

但是,如果我想根据 appSetting 部分的值获取密钥,该怎么办?

4

4 回答 4

0
 foreach (string key in Configuration.AppSettings.Settings.AllKeys)
        {
            string value = appSettings.Settings[key].Value;

            if (value.Equals(myValue))
            {
                 Console.WriteLine("Found Key: " + key);
                 break;
            }
        }
于 2013-03-19T11:06:23.617 回答
0

我会说你应该用值替换你的键,用键替换值,

<add  key="d:\TFS Projects-BotLogs/" value="botLogs"/>
<add  key="C:\BOTSCONFIG" value="botfolder"/>  

谢谢拉克什

于 2013-03-19T11:12:08.260 回答
0
foreach(string key in System.Configuration.ConfigurationSettings.AppSettings)
{   
 var key = key.ToString();   
 var value = ConfigurationSettings.AppSettings[key.ToString()];
 //Do something here
}

或者

var settings = System.Web.Configuration.ConfigurationSettings.AppSettings;

var value = from string k in settings.Keys
            where k.StartsWith("MyValue")
            select settings[k];
于 2013-03-19T11:04:27.683 回答
0

您可以尝试以下GetEnumerator method基于代码:

NameValueCollection values = ConfigurationManager.AppSettings;
Iterate(values);    
public void Iterate(NameValueCollection myCollection)
{
    IEnumerator myEnumerator = myCollection.GetEnumerator();
    Console.WriteLine("KEY VALUE");
    foreach (String item in myCollection.AllKeys)
    Console.WriteLine(" {0,-10} {1}", item, myCollection[item]);
    Console.WriteLine();
}
于 2013-03-19T11:04:43.197 回答