如果(我沒有檢查,),這是事實,你只是接受了錯誤的文件,你也許應該使用foreach或LINQ來檢查文件是否符合您的條件:
的foreach:
List<string> arrPaths = new List<string>();
Foreach(string strPath in Directory.GetFiles(filePath,".doc"))
{
if(strPath.EndsWith(".doc") & strPath.StartsWith("009"))
arrPaths.Add(strPath);
}
的Linq:
List<string> arrPaths = Directory.GetFiles(filePath,".doc").Where(pths => pths.StartsWith("009") && pths.EndsWith(".doc")).ToList();
這兩種方法都不止一個真正的解決方案要解決此問題,但我希望他們幫助:)
編輯
如果你想只得到文件名,我會從你的strPath中減去文件路徑是這樣的:
的foreach:
arrPaths.Add(strPath.Replace(filePath + "\\", ""));
的Linq:
List<string> arrPaths = Directory.GetFiles(filePath,".doc").Where(pt => pt.StartsWith("009") && pths.EndsWith(".doc")).Select(pths => pths.ToString().Replace(filePath + "\\", "").ToList();
是你**確定**你正在使用009 * .doc獲取所有文件?我會認真仔細檢查這個斷言與一個小測試程序... –
請重新格式化您的問題,並顯示更多的代碼。 – Alexander
[C#目錄可能有重複。GetFiles與掩碼](http://stackoverflow.com/questions/16664756/c-sharp-directory-getfiles-with-mask) – Set