如何显示 app.config 文件中的值

How to display the values from an app.config file

我有一个循环,当鼠标悬停在按钮上时,它会递增并显示递增数字。每个按钮的值都存储在 app.config file.how 中,我是否使用递增的值来显示正确的值名称?

当前代码

public void Controls()
{
    for (int i = 1; i <= Applications; i++)
    {
        string iconPath = ConfigurationManager.AppSettings["Application"+i+"_Icon"];
        Button button = new Button();

        this.Controls.Add(button);
        top += button.Height+25;
        button.Tag = i;
        button.MouseHover += Button_MouseHover;
        button.MouseLeave += Button_MouseLeave;
        button.Visible = true;

        button.Click += new EventHandler(button_Click);
   }

app.config:

 <appSettings>
  <add key="Applications" value="3" />

  <add key="Catergory" value="1"/>
  <add key="Application1_Name" value="word"/>
  <add key="Application1_Executable_Name" value="word.exe"/>
  <add key="Application1_AutoClose" value="true"/>
  <add key="Application1_Icon" value="C:\pictures\images.png"/>

   <add key="Catergory" value="1"/>
  <add key="Application2_Name" value="Paint"/>
  <add key="Application2_Executable_Path" value="C:\Windows\System32\"/>
  <add key="Application2_AutoClose" value="true"/>
  <add key="Application2_Icon" value="C:\pictures\images.png"/>


  <add key="Catergory" value="1"/>
  <add key="Application3_Name" value="notepad"/>
  <add key="Application3_Executable_Path" value="C:\Windows\"/>
  <add key="Application3_AutoClose" value="true"/>
  <add key="Application3_Icon" value="C:\pictures\images.png"/>

</appSettings>

目前,当我将鼠标悬停在按钮上时,它会显示值 1、2、3,但我希望它显示记事本、Word、画图。

我试过的

public void Controls()
{
        for (int i = 1; i <= Applications; i++)
        {
            NameValueCollection sAll;
            sAll = ConfigurationManager.AppSettings;

            Button button = new Button();

            this.Controls.Add(button);
            top += button.Height+25;
            button.Tag = i;

            foreach(string s in sAll.AllKeys)
            {
                button.Text = s + i ;
            }
        }
}

它的作用是向我显示 1、2、3 并为所有内容显示油漆。

试试这个

string value = ConfigurationManager.AppSettings["Key"];

因此,对于您的情况,使用 NameValueCollection 中的值,而不是键

    button.Text = sAll[s] + i ;

您的 s 变量实际上是 appSetting 键。您需要进一步传递此 Key 以获得相应的 appSettingConfigurationManager.AppSettings

        NameValueCollection sAll;
        sAll = ConfigurationManager.AppSettings;

        foreach (string s in sAll.AllKeys)
        {
            var applicationNamevalue = sAll [s];//Just for demonstration, should combine these statements.
            button.Text = applicationNamevalue + i; //and set it as button text.
        }

是的,@Shivang 的建议很好,如果你真的不关心键的值是什么,那么可以直接使用索引来获取配置值。

因为您需要密钥的值,所以您应该在循环中执行类似以下操作。

而且您不需要 foreach 循环。

按照下面的方法使用对你有帮助

因为您正在使用 foreach 循环,所以根本不需要。

所以删除它因为它会增加时间复杂度。

如果您的 App.config 中有多个密钥,但您想获得其中的一些密钥,那么您应该为 Application_Name 指定格式,例如:

  • application_name1
  • application_name2
  • application_name3
  • application_name4

然后在您的代码中执行以下操作,然后您只需将密钥添加到配置文件即可添加任意数量的按钮。

public void Controls()
    {
            for (int i = 1; i <= Applications; i++)
            {
                NameValueCollection sAll;
                sAll = ConfigurationManager.AppSettings;

                Button button = new Button();

                this.Controls.Add(button);
                top += button.Height+25;
                button.Tag = i;
                button.Text = sAll["application_name"+i] ;
            }
    }

在您的应用配置文件中,更好、更简洁的方法是像这样在配置部分添加应用程序名称

<configSections>
    <section name="ApplicationList" type="System.Configuration.NameValueSectionHandler" />
<configSections>

然后将连接到配置部分的每个应用程序名称添加

<ApplicationList>
    <add key="1" value="Notepad"/>
    <add key="2" value="Word"/>
    <add key="3" value="Paint"/>
</ApplicationList>

然后可以这样使用

var applications = ConfigurationManager.GetSection("ApplicationList") as NameValueCollection;
for (int i = 0; i < applications.Count; i++)
{
   var applicationName = applications[i].ToString() ;
}

然后转到 Controls 函数中的逻辑:如果 totalApplication 为您提供了所有当前应用程序,那么为什么要遍历 sAll.AllKeys 中的每个名称?

循环不应该这样编码吗

public void Controls()
  {
       var applications = ConfigurationManager.GetSection("ApplicationList") as NameValueCollection;
        for (int i = 0; i < applications.Count; i++)
        {
            var button = new Button();

            this.Controls.Add(button);
            top += button.Height+25;
            button.Tag = i;
            button.Text = applications[i].ToString();
        }
   }