如何使代码适用于所有行
本文关键字:适用于 何使 代码 | 更新日期: 2023-09-27 18:14:57
我有下一个代码,谁假设从checkedListBox
中选择checkbox
时更改文本文件中的行。代码工作,但只是对一些行,我不明白是什么原因,因为不适合所有。
private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string installerfilename = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "installer.ini");
IEnumerable<string> inilines = File.ReadAllLines(installerfilename).AsEnumerable();
string selectedItem = checkedListBox2.SelectedItem.ToString();
bool IsChecked = checkedListBox2.CheckedItems.Contains(selectedItem);
//bool IsChecked2 = inilines.Contains("#product=");
if (IsChecked)
inilines = inilines.Select(line => line == string.Format("#product={0}", selectedItem)
? Regex.Replace(line, string.Format("#product={0}", selectedItem), string.Format(@"product={0}", selectedItem))
: line);
else
inilines = inilines.Select(line => (line == string.Format("#product={0}", selectedItem) || line == string.Format(@"product={0}", selectedItem))
? Regex.Replace(line, string.Format(@".*product={0}", selectedItem), string.Format(@"#product={0}", selectedItem))
: line);
string strWrite = string.Join(Environment.NewLine, inilines.ToArray());
File.WriteAllText(installerfilename, strWrite);
}
一个不正常工作的例子是在接下来的行:当Aerospace Toolbox, Communications Blockset, Communications Toolbox在checkedListBox上被选中时,在installer.ini中我有以下更改:
#product=Aerospace Toolbox => product=Aerospace Toolbox
#product=Communications Blockset => product=Communications Blockset
#product=Communications Toolbox => remain the same #product=Communications Toolbox
有人能帮我吗?
我将更改以下内容
string selectedItem = checkedListBox2.SelectedItem.ToString();
bool IsChecked = checkedListBox2.CheckedItems.Contains(selectedItem);
bool IsChecked = checkedListBox2.CheckedItems.Contains(checkedListBox2.SelectedItem);