How to replace product to #product?

为君一笑 提交于 2019-12-25 03:44:13

问题


I have this code, but when I run the program remain blocked. With this code I want to check/uncheck all items, and when this are checked in text file installer.ini to write #product=name of checkbox => product=name of checkbox and if this are unchecked in text file where I have product=name of checkboxto replace with #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);
            }

        }
    }
}

回答1:


I'm sorry to say your question and code are not quite clear but, after studying for a while, I think I understand what you are trying to achieve and where the problem lies.

First, I think you are trying to check or uncheck all items using checkbox1. When all items are checked you want all products in your INI-file to be active (i.e. remove the comment sign (i.e. #)). When all items are unchecked you want all products to be inactive (i.e. add the comment sign).

Second, I assume your INI-file is quite large. Because you are going through all lines and save the INI-file after each line, you are saving the INI-file multiple times. This blocks the user interface until all lines have been processed.

Solution (based on your current approach)

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);
}

Tip

Take some time to study seperation of concerns or single responsibility. Your current code is doing too much (handling UI-events, doing IO and applying business logic) in one place.




回答2:


You can directly concatinate the strings

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


来源:https://stackoverflow.com/questions/32838492/how-to-replace-product-to-product

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!