当键需要增加值 1 时,如何使用循环将值添加到 app.config?

How can I add value to app.config with loop for, when key need to be increase about value 1?

我正在尝试使用循环 "for" 添加值路径,但还需要增加关于值 1 的键。 像这样:

<appSettings>
<add key="Path0" value="C:\Users\f.simora\Desktop\DP aplikacia />
<add key="Path1" value="C:\Users\f.simora\Desktop\example />
<add key="Path2" value="C:\Users\f.simora\Desktop\pause" />

我的代码不工作:

      private void btnAdd_Click(object sender, EventArgs e)
    {
        var appConf = System.Configuration.ConfigurationManager.AppSettings;
        int cnt = appConf.Count;
        if(cnt != 0)
        {
            for (int i = 0; i < cnt; i++)
            {
               if (i > cnt)
                { 
                    Configuration configuration = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
                    configuration.AppSettings.Settings.Add("Path" + i, textBox2.Text);
                    configuration.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                    dt.Rows.Add(textBox2.Text);
                    this.dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("Add is not complete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                } 
            }
        }
    }

你的条件有误

for (int i = 0; i < cnt; i++)
{
   if (i > cnt)

if 条件永远不会在您编写实际代码的地方执行。

您需要删除该条件

您的 if 块不正确,因为之前,在循环中,您正在增加 i 直到达到 cnt。当到达 for 循环的主体时,它不是 运行 因此 if 条件永远不会为真。

我认为您可能想将 if 块更改为 try-catch,这可能是您想要的流程:

try
{ 
    Configuration configuration = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    configuration.AppSettings.Settings.Add("Path" + i, textBox2.Text);
    configuration.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");

    dt.Rows.Add(textBox2.Text);
    this.dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
    MessageBox.Show("Add is not complete.\n" + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} 

此代码将尝试 运行,如果它遇到任何错误,它将捕获它们并显示在 MessageBox.

更新

我认为你的问题不仅在于一个循环,而且在你点击按钮之后,你有一个循环,每次都将你的 textBox2.Text 添加到 Path[i] 值,并且text 与它在循环中 *NOT** 相同。我不确定您想如何设置这些值,但您还需要了解,在您的循环中,您似乎只是在按钮单击事件的每个路径中添加相同的值。

也就是说,如果你想为设置添加全新的路径,你可以按如下方式进行:

var appConf = System.Configuration.ConfigurationManager.AppSettings;
var count = appConf.Count;

try
{
    // As OP commented this line fixed the problem
    value = null;

    Configuration configuration = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    configuration.AppSettings.Settings.Add("Path" + count, textBox2.Text);
    configuration.Save(ConfigurationSaveMode.Modified);

    ConfigurationManager.RefreshSection("appSettings");
    dt.Rows.Add(textBox2.Text);
    this.dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
    MessageBox.Show("Add is not complete.\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}