对于一些文本文件里面的内容进行,文本捡漏的问题写的一个处理程序
首先先创建一个程序框
如图:![在这里插入图片描述](https://img-blog.csdnimg.cn/79114a589f1344b59d143f21e668b3e2.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5bCP5p2J5p2JOTcxMTE1,size_11,color_FFFFFF,t_70,g_se,x_16)
在这里插入图片描述
如下代码段:
在这里插入代码片
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.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 文件_ID比较
{
public partial class index : Form
{
Dictionary<string, string> openWith1 = new Dictionary<string, string>();
Dictionary<string, string> openWith2 = new Dictionary<string, string>();
private static int sum = 1;
private static string str = "";
public index()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Title = "请选择标准ID的文件";
fileDialog.Filter = "所有文件(*.*)|*.*";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string file = fileDialog.FileName;
textBox1.Text = file;
}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Title = "请选择比对ID的文件";
fileDialog.Filter = "所有文件(*.*)|*.*";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string file = fileDialog.FileName;
textBox2.Text = file;
}
}
private void start_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("您还未指定操作文件","温馨提示");
}
else
{
timer.Start();
}
}
private void Flie_id1(string file_path)
{
int i = 0;
try
{
mess.Text = "加载标准文件中";
string str = File.ReadAllText(file_path, System.Text.Encoding.Default);
string[] str_temp = str.Split(new char[] { '\n' });
while (i < str_temp.Length - 1)
{
Console.WriteLine(str_temp[i]);
if (str_temp[i].Substring(0, 1).Equals(";") || str_temp[i].Length == 1 || str_temp[i].Length < 29 || openWith1.ContainsKey(str_temp[i].Substring(0, 19)) || str_temp[i].Substring(0, 2) != "0x")
{
i++;
continue;
}
else
{
string[] sArray = Regex.Split(str_temp[i], ", ", RegexOptions.IgnoreCase);
openWith1.Add(sArray[0].Trim().ToUpper(), str_temp[i]);
sum++;
i++;
}
}
}
catch
{
MessageBox.Show("文件读取:" + i.ToString() + "行出现问题", "温馨提示");
}
}
private void Flie_id2(string file_path)
{
int i = 0;
try
{
mess.Text = "加载标准文件中";
string str = File.ReadAllText(file_path, System.Text.Encoding.Default);
string[] str_temp = str.Split(new char[] { '\n' });
while (i < str_temp.Length - 1)
{
if (str_temp[i].Substring(0, 1).Equals(";") || str_temp[i].Length == 1 || str_temp[i].Length < 29 || openWith2.ContainsKey(str_temp[i].Substring(0, 19)) || str_temp[i].Substring(0, 2) != "0x")
{
i++;
continue;
}
else
{
string[] sArray = Regex.Split(str_temp[i], ", ", RegexOptions.IgnoreCase);
openWith2.Add(sArray[0].Trim().ToUpper(), str_temp[i]);
sum++;
i++;
}
}
}
catch
{
MessageBox.Show("文件读取:" + i.ToString() + "行出现问题", "温馨提示");
}
}
private void stop_Click(object sender, EventArgs e)
{
Flie_id1(textBox1.Text);
Flie_id2(textBox2.Text);
mess.Text = "文件加载完毕";
ProgressBar.Maximum = sum;
start.Enabled = true;
stop.Enabled = true;
}
private void timer_Tick(object sender, EventArgs e)
{
mess.Text = "正在对比";
foreach (string key in openWith1.Keys)
{
if (!openWith2.ContainsKey(key))
{
str += openWith1[key] + "\n";
}
ProgressBar.Value += ProgressBar.Step;
}
ProgressBar.Value = ProgressBar.Maximum;
mess.Text = "对比完成";
start.Enabled = false;
stop.Enabled = true;
File.WriteAllText(textBox3.Text, str);
openWith1.Clear();
openWith2.Clear();
timer.Stop();
}
private void index_Load(object sender, EventArgs e)
{
textBox3.Text = "FILE_ID.TXT";
}
}
}