【问题标题】:How to replace product to #product?如何将产品替换为#product?
【发布时间】:2015-09-30 19:52:11
【问题描述】:

我有这个代码,但是当我运行程序时仍然被阻止。使用此代码,我想检查/取消选中所有项目,并且当在文本文件 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);
            }

        }
    }
}

【问题讨论】:

  • “程序被阻止”是什么意思?你有什么例外吗?
  • 这段代码写在文本文件行中或者现在当我运行复选框时仍然被阻止..我不知道什么是粗糙的
  • 我只想用 product=..... 替换 #product= ...... 如果选中了复选框,如果不替换 product=..... with #product=...... 当我尝试检查和取消选中所有项目时,但当我尝试写入文本文件时不起作用

标签: c# checkbox


【解决方案1】:

很抱歉,您的问题和代码不是很清楚,但是经过一段时间的研究,我想我了解您要实现的目标以及问题所在。

首先,我认为您正在尝试使用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);
}

提示

花点时间学习seperation of concernssingle responsibility。您当前的代码在一个地方做了太多事情(处理 UI 事件、执行 IO 和应用业务逻辑)。

【讨论】:

  • 运行此代码时出现下一个错误:“进程无法访问文件”C:\....\installer.ini”,因为正在被另一个进程使用”
  • 可能是因为您使用记事本或其他编辑器打开了文件。
  • 我知道它已经过去了很长时间,但我还有另一个问题:(。出现该错误是因为在其他方法中我有字符串:“strWrite = string.Join(Environment.NewLine,inilines.ToArray( )); File.WriteAllText(installerfilename, strWrite);"。如何为此调用 saveInstaller,或者如何使代码正常工作?
  • 问题来了,如果你能回答我,我将不胜感激!stackoverflow.com/questions/32984830/…
【解决方案2】:

你可以直接连接字符串

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

【讨论】:

  • 现在文本文件中只有一行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多