如何将产品替换为#产品

本文关键字:产品 替换 | 更新日期: 2023-09-27 18:14:54

我有这段代码,但是当我运行程序时仍然被阻塞。使用此代码,我想检查/取消检查所有项目,当在文本文件installer.ini中检查#product=name of checkbox => product=name of checkbox时,如果在文本文件中未检查product=name of checkbox,则用#product=name of checkbox替换。

private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
{
    string installerfilename = path + "installer.ini";
    string installertext = File.ReadAllText(installerfilename);
    var lin =File.ReadLines(path + "installer.ini").ToArray();
    CheckBox cb = sender as CheckBox;
    if (cb.Checked)
    {
        for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
        {
            this.checkedListBox2.SetItemChecked(i, true);
            foreach (var txt in lin)
            {
                if (txt.Contains("#product="))
                {
                    var name = txt.Split('=')[1];
                    installertext = installertext.Replace("#product=", "product=" + name);
                }
                File.WriteAllText(installerfilename, installertext);
            }
        }
    }
    else if (!cb.Checked)
    {
        for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
        {
            this.checkedListBox2.SetItemChecked(i, false);
            foreach (var txt in lin)
            {
                if (txt.Contains("product="))
                {
                    var name1 = txt.Split('=')[1];
                    installertext = installertext.Replace("product=", "#product=" + name1);
                }
                File.WriteAllText(installerfilename, installertext);
            }
        }
    }
}

如何将产品替换为#产品

我很抱歉地说你的问题和代码不是很清楚,但是,学习了一段时间后,我想我明白你想要实现的目标和问题所在。

首先,我认为您正在尝试使用checkbox1检查或取消检查所有项目。当选中所有项目时,您希望ini文件中的所有产品都是活动的(即删除注释符号(即#))。当所有项目都未选中时,您希望所有产品都处于非活动状态(即添加注释符号)。

第二,我假设您的ini文件相当大。因为要遍历所有行并在每行之后保存ini文件,所以要多次保存ini文件。这将阻塞用户界面,直到所有行都被处理完。

解决方案(基于您当前的方法)

private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
{
    CheckBox cb = sender as CheckBox;
    SetAllItemsChecked(cb.Checked);
    var installerLines = ReadInstallerLines();
    SetAllProductsChecked(installerLines.ToList(), cb.Checked);
    SaveInstaller(installerLines);
}
private void SetAllItemsChecked(bool check)
{
    for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
    {
        this.checkedListBox2.SetItemChecked(i, check);
    }       
}
private IEnumerable<string> ReadInstallerLines()
{
    string installerfilename = path + "installer.ini";
    string installertext = File.ReadAllText(installerfilename);
    return File.ReadLines(path + "installer.ini");
}
private void SetAllProductsChecked(IList<string> installerLines, bool check)
{
    for (var i = 0; i < installerLines.Count; i++)
    {
        if (installerLines[i].Contains("product="))
        {
            installerLines[i] = check 
                ? installerLines[i].Replace("#product", "product") 
                : installerLines[i].Replace("product", "#product");
        }
    }
}
private void SaveInstaller(IEnumerable<string> installerLines)
{
    string installerfilename = path + "installer.ini";
    File.WriteAllLines(installerfilename, installerLines);
}
小费

花点时间研究关注点分离或单一责任。你当前的代码在一个地方做了太多的事情(处理ui事件,做IO和应用业务逻辑)

您可以直接连接字符串

 var name1 = txt.Split('=')[1];
 installertext =  "#product=" + name1;