C# WINWORD.exe 不会退出 - 目录文件升序排序

C# WINWORD.exe won't exit - Sort Directory Files assending

我不是 C# 专家,但我尽力了, 这是一个小型控制台应用程序,应该获得 2 个 arg2(通过将 args 传递给 exe 或通过控制台输入)我有 2 个问题,我找不到任何其他解决方案

  1. 合并文件的顺序不正确,如果文件名有 包括字母。前任。 (1.docx , 2.docx , 3.docx ) 有效 => result.docx(1,2,3) (1test.docx , 2rice.docx , 3john.docx ) => result.docx(3,1,2)
  2. 在其应用程序完成后无法让 WINWORD.exe 关闭 完成

PS: This exe is being called by PHP line CMD.EXE

我尝试了所有可能的命令来释放 com 对象然后关闭应用程序, 我的错误是什么?如何正确优化代码?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.Runtime.InteropServices;
using System.IO;

namespace DocEngine
{
    public class Parameter
    {
        public string Name;
        public string Value;


        // Note we need to give a default constructor when override it
        public Parameter()
        {

        }

    }

    class Program
    {
        public static void MergeDocuments(string fileName, List<string> documentFiles)
        {

            _Application oWord = new Microsoft.Office.Interop.Word.Application();
            _Document oDoc = oWord.Documents.Add();
            Selection oSelection = oWord.Selection;            

            foreach (string documentFile in documentFiles)
            {
                _Document oCurrentDocument = oWord.Documents.Add(documentFile);
                CopyPageSetup(oCurrentDocument.PageSetup, oDoc.Sections.Last.PageSetup);
                oCurrentDocument.Range().Copy();
                oSelection.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
                if (!Object.ReferenceEquals(documentFile, documentFiles.Last()))
                    oSelection.InsertBreak(WdBreakType.wdSectionBreakNextPage);
            }

            oDoc.SaveAs(fileName, WdSaveFormat.wdFormatDocumentDefault);
            oDoc.Close();                      

            //TODO: release objects, close word application
            //Marshal.ReleaseComObject(oSelection);
            //Marshal.ReleaseComObject(oDoc);
            //Marshal.ReleaseComObject(oWord);
            Marshal.FinalReleaseComObject(oSelection);
            Marshal.FinalReleaseComObject(oDoc);
            Marshal.FinalReleaseComObject(oWord);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oWord);


                System.Runtime.InteropServices.Marshal.ReleaseComObject(oSelection);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
                oSelection = null;
                oDoc = null;
                oWord = null;

                // A good idea depending on who you talk to...
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();


        }


        public static void CopyPageSetup(PageSetup source, PageSetup target)
        {
            target.PaperSize = source.PaperSize;

            //target.Orientation = source.Orientation; //not working in word 2003, so here is another way
            if (!source.Orientation.Equals(target.Orientation))
                target.TogglePortrait();

            target.TopMargin = source.TopMargin;
            target.BottomMargin = source.BottomMargin;
            target.RightMargin = source.RightMargin;
            target.LeftMargin = source.LeftMargin;
            target.FooterDistance = source.FooterDistance;
            target.HeaderDistance = source.HeaderDistance;
            target.LayoutMode = source.LayoutMode;
        }


        static void Main(string[] args)
        {
            if (args == null || args.Length == 0)
              {
                Console.WriteLine("Enter Path:");
                Parameter parameter1 = new Parameter
                {
                    Name = "dir",
                    Value = Console.ReadLine()
                };

                Console.WriteLine("FileName without ext:");
                Parameter parameter2 = new Parameter
                {
                    Name = "fileName",
                    Value = Console.ReadLine()
                };
                Console.WriteLine("Thank you! ");

                Console.WriteLine("Test parameter1: [{0}] = [{1}]", parameter1.Name, parameter1.Value);
                Console.WriteLine("Test parameter2: [{0}] = [{1}]", parameter2.Name, parameter2.Value);

                try
                {

                    List<string> result = Directory.EnumerateFiles(parameter1.Value, "*.doc", SearchOption.AllDirectories).Union(Directory.EnumerateFiles(parameter1.Value, "*.docx", SearchOption.AllDirectories)).ToList();
                    var filename = Path.Combine(parameter1.Value, parameter2.Value);

                    MergeDocuments(filename, result);
                }
                catch (UnauthorizedAccessException UAEx)
                {
                    Console.WriteLine(UAEx.Message);
                }
                catch (PathTooLongException PathEx)
                {
                    Console.WriteLine(PathEx.Message);
                }
            }

           else
            {

                //args = new string[2];

                string sourceDirectory = args[0];
                string filename1 = args[1];

                try
                {
                    List<string> result = Directory.EnumerateFiles(sourceDirectory, "*.doc", SearchOption.AllDirectories).Union(Directory.EnumerateFiles(sourceDirectory, "*.docx", SearchOption.AllDirectories)).ToList();
                    var filename = Path.Combine(sourceDirectory, filename1);

                    MergeDocuments(filename, result);
                }
                catch (UnauthorizedAccessException UAEx)
                {
                    Console.WriteLine(UAEx.Message);
                }
                catch (PathTooLongException PathEx)
                {
                    Console.WriteLine(PathEx.Message);
                }


            }
        }
    }

}

您不需要进行任何这些 COM 调用或显式 GC 调用,也不需要将内容显式设置为 null。您只需致电 Application.Quit.