0

I am learning C# and I would like some help. With the help of the people on I have managed to achieve editing an .inf file section using a regex, but I would like to keep the original line of text before editing. I have placed the code below and a sample of the .INF data before and if possible what I would like it to look like, but not too bothered with how it will look as long as the data is there.

else if (!chkbxintdial.Checked)
{
   var lines = File.ReadAllLines(txtcurrent.Text).ToList();
   var regex = new Regex(@"^[00]{2}");
   var startFrom = lines.IndexOf("[Destinations]");

   //Start replacing from the index of "[Destinations]"
   for (int i = startFrom; i < lines.Count; i++)
   {
      //Assuming the ini section ends at an empty line - stop replacing
      if (lines[i].Trim() == string.Empty)
      break;

      lines[i] = regex.Replace(lines[i], "+");
   }
   File.WriteAllLines(txtnewtariff.Text, lines);

Sample of what I would like to do.

Original file

[Destinations]
00="International",I,IV
0024195="Gabon Mobiles",I,IMT
0024197="Gabon Mobiles",I,IMT

After Process.

[Destinations]
00="International",I,IV
+="International",I,IV
0024195="Gabon Mobiles",I,IMT
+24195="Gabon Mobiles",I,IMT
0024197="Gabon Mobiles",I,IMT
+24197="Gabon Mobiles",I,IMT
4

1 回答 1

3

而不是完全替换该行,而是添加到该行:

lines[i] += Environment.NewLine + regex.Replace(lines[i], "+");

还值得注意的是,存在专门用于操作 .INF 文件的工具,因此除了出于学术目的之外,实际上没有必要这样做。虽然您的解决方案(除了这个小修复)似乎可以工作,但它对于较大的文件并不是特别有效,在面对任意数据时也不是特别健壮。

于 2013-04-23T19:26:08.570 回答