FileFilter 遍历某个目录下文件名含有某个字符的文件
由于IIS版本的升级,造成了文件名中含有“+”的特殊字符的文件(多数是图片)在网页中不能被访问,于是必须查找当前目录下含有多少这样的文件,从而制定最佳的解决方案。
废话少说,直接上核心代码:
public void GetAllFilesInDirectory(string strDirectory, string searchCh) { DirectoryInfo directory = new DirectoryInfo(strDirectory); DirectoryInfo[] directoryArray = directory.GetDirectories(); FileInfo[] fileInfoArray = directory.GetFiles(); foreach (var item in fileInfoArray) { if (item.Name.IndexOf(searchCh) > -1) { ... ... } } foreach (DirectoryInfo _directoryInfo in directoryArray) { DirectoryInfo directoryA = new DirectoryInfo(_directoryInfo.FullName); TreeNode nodeChild = new TreeNode(directoryA.Name); DirectoryInfo[] directoryArrayA = directoryA.GetDirectories(); FileInfo[] fileInfoArrayA = directoryA.GetFiles(); GetAllFilesInDirectory(_directoryInfo.FullName, searchCh); } }
下面是我做的一个实例,界面是winform
程序源码:点我下载