比對兩個Word檔案內容是否一致的C#解決辦法

來源:互聯網
上載者:User

標籤: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;            }        }    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.