我找到了我的問題的答案。下面列出了一個示例代碼和解釋。
您必須爲此使用以下內容!
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