大体界面如下:
OpenFileA按钮的单击事件代码如下:
private void btnOpenA_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "txt文件(*.txt)|*.txt"; if (openFileDialog.ShowDialog() == DialogResult.OK) { this.txtFileA.Text = openFileDialog.FileName.ToLower(); //得到的文件名包含路径 } }
OpenFileB按钮的单击事件代码如下:
private void btnOpenB_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "txt文件(*.txt)|*.txt"; if (openFileDialog.ShowDialog() == DialogResult.OK) { this.txtFileB.Text = openFileDialog.FileName.ToLower(); //得到的文件名包含路径 } }
FileA-FileB按钮的单击事件代码如下:
注意:FileA-FileB是指那些包含在A中但是不包含在B中的文件A的内容,注意该例中的文件都是文本文件,文件的内容是单列。
private void btnADiffB_Click(object sender, EventArgs e) { string[] linesA = System.IO.File.ReadAllLines(this.txtFileA.Text); List<string> listA = linesA.ToList(); string[] linesB = System.IO.File.ReadAllLines(this.txtFileB.Text); List<string> listB = linesB.ToList(); for (int i = 0; i < listB.Count; i++) { if (listA.Contains(listB[i])) { listA.Remove(listB[i]); } } FileStream fs = File.Open(@"d:\FileADiffB.txt", FileMode.Create); StreamWriter sw= new StreamWriter(fs); for (int i = 0; i < listA.Count; i++) { sw.WriteLine(listA[i]); } sw.Flush(); sw.Close(); fs.Close(); this.lbResult.Text = "file A-B have write to d:\\FileADiffB.txt"; }
FileB-FileA按钮的单击事件代码如下:
注意:FileB-FileA是指那些包含在B中但是不包含在A中的文件B的内容,注意该例中的文件都是文本文件,文件的内容是单列。
private void btnBDiffA_Click(object sender, EventArgs e) { string[] linesA = System.IO.File.ReadAllLines(this.txtFileA.Text); List<string> listA = linesA.ToList(); string[] linesB = System.IO.File.ReadAllLines(this.txtFileB.Text); List<string> listB = linesB.ToList(); for (int i = 0; i < listA.Count; i++) { if (listB.Contains(listA[i])) { listB.Remove(listA[i]); } } FileStream fs = File.Open(@"d:\FileBDiffA.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); for (int i = 0; i < listB.Count; i++) { sw.WriteLine(listB[i]); } sw.Flush(); sw.Close(); fs.Close(); this.lbResult.Text = "file B-A has been writed to d:\\FileBDiffA.txt"; }
本文出自 “花开花落” 博客,谢绝转载!
原文:http://020618.blog.51cto.com/6098149/1742585