两个文本进行比对后,达到删除多余项的目的
在Exercise.exer和 Unit.ut文件中,有两个id号,正常情况下两文件中id的个数相同,当不同时候需要删除相关的内容。通过比对最后删除Exercise.exer文件中的相应内容 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms; namespace WindowsFormsApplication2{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string path = txtPath.Text + @"\Exercise.exer"; string[] contents = File.ReadAllLines(path, Encoding.UTF8); List<string> outContents = new List<string>();//最后需要输出的文本 List<int> startLineIndex = new List<int>(); List<int> endLineIndex = new List<int>(); List<int> moveStartIndex = new List<int>(); List<int> moveEndIndex = new List<int>(); List<string> id = new List<string>(); for (int i = 0; i < contents.Length; i++) { if ((contents.Trim()).StartsWith("<Evaltype=")) { startLineIndex.Add(i);//14==endLineIndex-startLineIndex; } if ((contents.Trim()).StartsWith("</Eval>")) { endLineIndex.Add(i); } if ((contents.Trim()).StartsWith("<Evaluationid=")) { string[] str = contents.Split("\"".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); id.Add(str); } } string path2 = txtPath.Text + @"\Unit.ut"; string[] contents2 = File.ReadAllLines(path2, Encoding.UTF8); List<string> id2 = new List<string>(); for (int i = 0; i < contents2.Length; i++) { if ((contents2.Trim()).StartsWith("<EvaluationID>")) { string[] str = contents2.Split("><".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); id2.Add(str); } } for (int i = 0; i < startLineIndex.Count;i++) { if (id2.Contains(id) == false) { moveStartIndex.Add(startLineIndex); moveEndIndex.Add(endLineIndex); } } if (moveStartIndex.Count==0) { MessageBox.Show("验证成功!\t\n有效节点数为:" + id2.Count.ToString() + "\t\n实际节点数为:" + id.Count.ToString() ); } else if (moveStartIndex.Count > 0) { if (MessageBox.Show("验证失败!\t\n有效节点数为:" + id2.Count.ToString() + "\t\n实际节点数为:" + id.Count.ToString() + "\t\n需要进行修改吗?", "Confirm Message", MessageBoxButtons.OKCancel) == DialogResult.OK) { //delete for (int i = 0; i < moveStartIndex.Count;i++) { if (i == 0) { for (int j = 0; j < moveStartIndex;j++) { outContents.Add(contents); } } else { for (int j = moveEndIndex + 1; j <moveStartIndex; j++) { outContents.Add(contents); } } if (i == moveEndIndex.Count - 1) { for (int j = moveEndIndex + 1; j <contents.Length; j++) { outContents.Add(contents); } } } //outContents输出到目标里面即可! File.WriteAllLines(path,outContents, Encoding.UTF8); MessageBox.Show("修改完成!"); } } } private void button2_Click(object sender, EventArgs e) { string fName; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\ openFileDialog.Filter = "练习文件|*.exer"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog() == DialogResult.OK) { fName =openFileDialog.FileName; txtPath.Text=Path.GetDirectoryName(fName); } } }}楼主好人! good 看了,觉得云山雾罩的。 {:1106_400:}
太牛了! 謝謝樓主分享!!
不错的功能! 不是有软件么,自己编这个做什么用的 fanzju 发表于 2015-12-4 17:07
不是有软件么,自己编这个做什么用的
软件是辅助啊,你要用来编程啊 maboy 发表于 2015-12-4 10:15
不错的功能!
{:1106_362:} horling 发表于 2015-12-4 08:14
謝謝樓主分享!!
{:1106_367:}
123wys 于 2015-12-06 20:31:58 补充以下内容:
谢谢 {:1106_362:}反正是挺厉害的。。。
页:
[1]