C#:如何从一个文件夹中打开多个word文档
C#: How to open multiple word documents from a folder
我有一个文件夹,其中包含名为 Test1、Test2 和 Test3 的三个 word 文档,所有文档都具有 docx 扩展名。
我知道如果我们想手动打开一个文件,我们可以指定一个文件路径并使用下面的代码:
Word.Application fileOpen = new Word.Application();
Word.Document document = fileOpen.Documents.Open(filePath);
但是,有没有其他方法可以 select 该文件夹,但一次打开一个文件?我想做以下事情:
- 打开 Test1.docx -> 进行一些更改 -> 保存 -> 关闭
- 打开Test2.docx -> 重复上面的操作,最后对Test3.docx
做同样的事情
我到处搜索,但找不到与此相关的内容。如有任何帮助,我们将不胜感激。
对于这种情况,我们假设您已经知道要打开哪个文件夹并从中读取文件。
首先,您必须导入 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.
}
}
希望这足以让您入门。祝你好运。
我找到了问题的答案。我在下面包含了示例代码和解释。
您必须为此使用以下内容!
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 Interops 打开每个文件并进行更改等。
我有一个文件夹,其中包含名为 Test1、Test2 和 Test3 的三个 word 文档,所有文档都具有 docx 扩展名。
我知道如果我们想手动打开一个文件,我们可以指定一个文件路径并使用下面的代码:
Word.Application fileOpen = new Word.Application();
Word.Document document = fileOpen.Documents.Open(filePath);
但是,有没有其他方法可以 select 该文件夹,但一次打开一个文件?我想做以下事情:
- 打开 Test1.docx -> 进行一些更改 -> 保存 -> 关闭
- 打开Test2.docx -> 重复上面的操作,最后对Test3.docx 做同样的事情
我到处搜索,但找不到与此相关的内容。如有任何帮助,我们将不胜感激。
对于这种情况,我们假设您已经知道要打开哪个文件夹并从中读取文件。
首先,您必须导入 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.
}
}
希望这足以让您入门。祝你好运。
我找到了问题的答案。我在下面包含了示例代码和解释。
您必须为此使用以下内容!
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 Interops 打开每个文件并进行更改等。