2017-06-29 124 views
-4

我有一个文件夹,其中包含三个名为Test1,Test2和Test3的word文档,所有这些文档都带有docx扩展名。如何打开文件夹中的多个word文档

我知道,如果我们要手动打开一个文件,我们可以指定文件路径,并使用下面的代码:

Word.Application fileOpen = new Word.Application(); 
Word.Document document = fileOpen.Documents.Open(filePath); 

但是否有任何其他的方式来选择文件夹,但在同一时间打开一个文件?我想做到以下几点:

  1. 打开Test1.docx - >做一些修改 - >保存 - >关闭
  2. 打开Test2.docx - >重复以上,最后做Test3的同.docx

我到处搜索,但无法找到与此相关的内容。任何帮助将不胜感激,谢谢。

回答

0

我找到了我的问题的答案。下面列出了一个示例代码和解释。

您必须为此使用以下内容!

Using System.IO; 

在这个例子中,我们将看看桌面目录中的所有“doc”和“docx”文件。

//Directory to search through: 
string sampleFilePath = @"C:\Users\YourUsername\Desktop"; 

//Now we create a list to hold all the filepaths that exist in this directory 
List<string> allFilePaths = new List<string>(); 
//Extract all doc/docx files on Desktop 
string[] start = Directory.GetFiles(sampleFilePath, "*doc"); 
     for (int begin = 0; begin < start.Length; begin++) 
     { 
      allFilePaths.Add(start[begin]); 
     } 

//The above code does not extract doc/docx files that are contained inside folders that exist on desktop. 
//So we need to get those filepaths separately. 
string[] filePaths = Directory.GetDirectories(sampleFilePath); 
     for (int i = 0; i < filePaths.Length; i++) 
     { 
      //Any doc and docx files found in the subdirectories are added to the list 
      string[] files = Directory.GetFiles(filePaths[i], "*doc"); 
      for (int j = 0; j < files.Length; j++) 
      { 
       allFilePaths.Add(files[j]); 
      } 
      //Continue checking the subdirectories for more folders until you reach the end, then move on to the second subdirectory from the very beginning. 
      string[] checkMoreDirectories = Directory.GetDirectories(filePaths[i]); 
      checkForMoreDirectories(checkMoreDirectories, ref allFilePaths); 
     } 

checkForMoreDirectories方法在下面给出。

static void checkForMoreDirectories (string[] checkMoreDirectories, ref List<string> myList) 
    { 
     //This function calls itself recursively for every subdirectory within the previous subdirectory until it reaches the end where there are no more folders left 
     //Continuously add any doc/docx files found before going deeper into the subdirectory 
     for (int i = 0; i < checkMoreDirectories.Length; i++) 
     { 
      string[] files = Directory.GetFiles(checkMoreDirectories[i]); 
      for (int j = 0; j < files.Length; j++) 
      { 
       myList.Add(files[j]); 
      } 
      string[] temp = Directory.GetDirectories(checkMoreDirectories[i]); 
      //Recursive call 
      checkForMoreDirectories(temp, ref myList); 
     } 
    } 

现在,即使你有每个1000个文件夹与存储DOC/DOCX文件很多子,你将不得不为那些存储在列表中的每一个文件路径。然后,您可以访问此列表,并通过使用Microsoft Word Interop打开每个文件并进行更改等。

+5

Hé@Dylan,其他答案的OP在他们根据您的意见提供答案时预期得到不同的结果。如果你觉得这是你介意在[meta](https://meta.stackoverflow.com/q/351673/578411)?。我们不咬... – rene

+0

这里有几件事要提到。 1.我投了他的答案,但它说,“投票少于15声望的投票记录,但不公开更改显示”。 2.是的,他的回答是正确的,我的确如我在1中提到的那样。 3.我发布的代码与他的显着不同,它适用于存在于另一个中的较大规模的子文件夹。 – Dylan

+0

是的,声誉要求确实被评论者注意到了,并且我在meta回答中明确表达了这一点。你需要一个文件夹的解决方案并不清楚我认为这个问题是否让其他答复者感到意外。 AFAICT现在我们都很好。谢谢你的评论,享受你的一天。 – rene

4

对于这种情况,我们假设您已经知道您想从哪个文件夹打开&从中读取文件。

首先,你必须导入System.IO

using System.IO; 

在该命名空间,Directory.GetFiles()会给你一个字符串数组中的文件名。

private static void FolderReader() 
{ 
    string folderPath = @"C:\someFolder\"; 
    Word.Application fileOpen = new Word.Application(); 
    string[] filePaths = Directory.GetFiles(folderPath); 

    foreach (string filePath in filePaths) 
    { 
     Word.Document document = fileOpen.Documents.Open(filePath); 
     // perform continue with your algorithm... 
     // close the file when you're done. 
    } 
} 

希望这足以让你开始。祝你好运。