C#:如何从文件夹

时间:2017-06-29 19:11:56

标签: c# visual-studio ms-word automation office-interop

我有一个包含三个Word文档的文件夹,名为Test1,Test2和Test3,所有这些文档都带有docx扩展名。

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

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

但是有没有其他方法可以选择该文件夹,但一次打开一个文件?我想做以下事情:

  1. 打开Test1.docx - >做一些改变 - >保存 - >靠近
  2. 打开Test2.docx - >重复上面,最后对Test3.docx
  3. 做同样的事情

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

2 个答案:

答案 0 :(得分: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.
    }
}

希望这足以让你入门。祝你好运。

答案 1 :(得分:0)

我找到了问题的答案。我在下面列出了示例代码和说明。

您必须使用以下内容!

Using System.IO;

对于此示例,我们将查看从Desktop目录中获取所有“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 Interops打开每个文件并进行更改等。