以梦为马,不负韶华

搜索
查看: 2189|回复: 11
收起左侧

两个文本进行比对后,达到删除多余项的目的

[复制链接]
发表于 2015-12-3 21:28:50 显示全部楼层 |阅读模式
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[j]-startLineIndex[j];
                }
                if ((contents.Trim()).StartsWith("</Eval>"))
                {
                   endLineIndex.Add(i);
                }
                if ((contents.Trim()).StartsWith("<Evaluationid="))
                {
                    string[] str = contents.Split("\"".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    id.Add(str[1]);
                }
            }
            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[2]);
                }
            }
            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[0];j++)
                            {
                                outContents.Add(contents[j]);
                            }
                        }
                        else
                        {
                            for (int j = moveEndIndex[i - 1] + 1; j <moveStartIndex; j++)
                            {
                               outContents.Add(contents[j]);
                            }
                        }
                        if (i == moveEndIndex.Count - 1)
                        {
                            for (int j = moveEndIndex + 1; j <contents.Length; j++)
                            {
                               outContents.Add(contents[j]);
                            }
                        }
                    }
                    //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);
               
            }
        }
    }
}

发表于 2015-12-3 21:28:50 显示全部楼层
good                                                     
回复 支持 反对

使用道具 举报

发表于 2015-12-3 21:28:50 显示全部楼层
看了,觉得云山雾罩的。
回复 支持 反对

使用道具 举报

发表于 2015-12-3 21:28:50 显示全部楼层
謝謝樓主分享!!


回复 支持 反对

使用道具 举报

发表于 2015-12-3 21:28:50 显示全部楼层
不错的功能!
回复 支持 反对

使用道具 举报

发表于 2015-12-3 21:28:50 来自手机 显示全部楼层
不是有软件么,自己编这个做什么用的

点评

软件是辅助啊,你要用来编程啊  详情 回复 发表于 2015-12-6 20:31
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-3 21:28:50 显示全部楼层
fanzju 发表于 2015-12-4 17:07
不是有软件么,自己编这个做什么用的

软件是辅助啊,你要用来编程啊
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-3 21:28:50 显示全部楼层

{:1106_362:}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-3 21:28:50 显示全部楼层

{:1106_367:}

123wys 于 2015-12-06 20:31:58 补充以下内容:
谢谢
回复 支持 反对

使用道具 举报

发表于 2015-12-3 21:28:50 显示全部楼层
{:1106_362:}反正是挺厉害的。。。
回复 支持 反对

使用道具 举报

不想打字就选择快捷回复吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|以梦为马,不负韶华

GMT+8, 2025-4-14 01:25

Powered by 以梦为马,不负韶华

© 2024-2099 Meng.Horse

快速回复 返回顶部 返回列表