日期:2014-05-18 浏览次数:20452 次
string strPath = @"D://数据管理/"; System.Collections.Generic.List<string> searchNames=new System.Collections.Generic.List<string>(); getFileNames(strPath, "1", searchNames); return searchNames; // 此时的searchNames已经获取到了所有文件名 ... ... /// <summary> /// 根据条件为searchNames赋值 /// </summary> /// <param name="strPath">给定路径</param> /// <param name="strKeyWords">给定查询关键字</param> /// <param name="searchNames">要存放的List</param> private void getFileNames(string strPath, string strKeyWords, System.Collections.Generic.List<string> searchNames) { string[] direcNames = Directory.GetDirectories(strPath); string[] fileNames = Directory.GetFiles(strPath); for (int i = 0; i < fileNames.Length; i++) { if (fileNames[i].Contains(strKeyWords)) { searchNames.Add(fileNames[i]); } } for (int i = 0; i < direcNames.Length; i++) { getFileNames(direcNames[i], strKeyWords, searchNames); } }
------解决方案--------------------
string[] strName= System.IO.Directory.GetFiles("D://数据管理", "关键字", System.IO.SearchOption.AllDirectories); List<string> list=new List<string>(); foreach (string s in strName) { if (s.EndsWith(".txt")) list.Add(s); }