我找到了我的问题的答案。下面列出了一个示例代码和解释。
您必须为此使用以下内容!
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打开每个文件并进行更改等。
Hé@Dylan,其他答案的OP在他们根据您的意见提供答案时预期得到不同的结果。如果你觉得这是你介意在[meta](https://meta.stackoverflow.com/q/351673/578411)?。我们不咬... – rene
这里有几件事要提到。 1.我投了他的答案,但它说,“投票少于15声望的投票记录,但不公开更改显示”。 2.是的,他的回答是正确的,我的确如我在1中提到的那样。 3.我发布的代码与他的显着不同,它适用于存在于另一个中的较大规模的子文件夹。 – Dylan
是的,声誉要求确实被评论者注意到了,并且我在meta回答中明确表达了这一点。你需要一个文件夹的解决方案并不清楚我认为这个问题是否让其他答复者感到意外。 AFAICT现在我们都很好。谢谢你的评论,享受你的一天。 – rene