2016-11-18 48 views
-1

我不是很擅長C#,但我試圖盡我所能, 這是一個小的控制檯應用程序,應該得到2 arg2(通過傳遞args到EXE或通過控制檯輸入)我有2個問題,它和我無法找到任何其他解決方案C#WINWORD.exe將不會退出 - 排序目錄文件

  1. 合併的文件是不是在正確的順序,如果文件名具有 字母包括在內。恩。 (1docx,2.docx,3john.docx)=> result.docx(1,2,3) 1,2)
  2. 無法獲得WINWORD.EXE到其appliaction後關閉的 完成

PS:這個exe文件被PHP線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); 
       } 


      } 
     } 
    } 

} 

回答

2

你不需要做任何的COM調用或顯式GC的電話,而你並不需要明確設置的事情空。你只需要撥打Application.Quit

+0

我想這一點,WINWORD.EXE仍然存在:(, – Ti2

+0

我會建議減少您的問題,並重建它一塊一塊。與少了點'新Microsoft.Office.Interop.Word.Application應用程序首次啓動() ;'後面加上'oWord.Quit();'驗證它終止,然後加入'oWord.Documents.Add()'和'oDoc.Close()',確認它終止。 –

+0

我試過這個((_Application)oWord).Quit();它的工作原理,我也評論了所有的元帥電話和GC調用,非常感謝 – Ti2