码迷,mamicode.com
首页 > 其他好文 > 详细

比对两个Word文件内容是否一致的C#解决办法

时间:2014-07-29 16:51:42      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   文件   io   for   art   

using System;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Office.Interop.Word;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            //加载之前首先判断系统中是否包含WORD.EXE进程,如果包含,将其杀死,然后再进行查看
            KillProcess();

            MessageBox.Show(CompareWordFile(@"C:\1.DOC", @"C:\2.DOC").ToString());
        }

        public bool CompareWordFile(String source, String target)
        {
            object filename = source;
            var targetFileName = target;
            object missing = System.Reflection.Missing.Value;
            object readonlyobj = false;
            var app = new ApplicationClass { Visible = false };
            var doc = app.Documents.Open(ref filename, ref missing, ref readonlyobj, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            doc.TrackRevisions = true;
            doc.ShowRevisions = true;
            doc.PrintRevisions = true;
            object comparetarget = WdCompareTarget.wdCompareTargetNew;
            doc.Compare(targetFileName, ref missing, ref comparetarget, ref missing, ref missing, ref missing, ref missing, ref missing);
            var changeCount = app.ActiveDocument.Revisions.Count;
            Object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
            doc.Close(ref saveChanges, ref missing, ref missing);
            app.Quit(ref saveChanges, ref missing, ref missing);
            return changeCount == 0;
        }

        public void KillProcess()
        {
            const string processName = "WINWORD";
            var process = Process.GetProcessesByName(processName);
            try
            {
                foreach (var p in process)
                {
                    p.Kill();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请先关闭系统中的WINWORD.EXE进程!", "文件对比失败", MessageBoxButtons.OK);
                return;
            }
        }
    }
}

 

比对两个Word文件内容是否一致的C#解决办法,布布扣,bubuko.com

比对两个Word文件内容是否一致的C#解决办法

标签:style   blog   color   os   文件   io   for   art   

原文地址:http://www.cnblogs.com/littlehb/p/3875493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!