如何从一个指定的目录下,递归的获取这个目录以及子目录下的所有dll文件呢?(也可以是其他类型的文件或是所有文件),我想,很多同志们都遇到过这个问题,为了避免重复发明轮子,我将这段代码贴出来,供大家参考:
1 /// <summary>
2 /// 从指定的目录下,递归搜索dll文件
3 /// </summary>
4 /// <param name="dllPath">dll文件所在的路径</param>
5 /// <returns>包含绝对路径的dll文件集合</returns>
6 public static IList<string> GetAllDllFileFromDir(DirectoryInfo directory)
7 {
8 IList<string> result = new List<string>();
9
10 foreach (FileInfo file in directory.GetFiles("*.dll"))
11 {
12 result.Add(file.FullName);
13 }
14
15 DirectoryInfo[] subDirectories = directory.GetDirectories();
16
17 foreach (DirectoryInfo subDirectory in subDirectories)
18 {
19 GetAllDllFileFromDir(subDirectory);
20 }
21
22 return result;
23 }
下面是使用范例:2 /// 从指定的目录下,递归搜索dll文件
3 /// </summary>
4 /// <param name="dllPath">dll文件所在的路径</param>
5 /// <returns>包含绝对路径的dll文件集合</returns>
6 public static IList<string> GetAllDllFileFromDir(DirectoryInfo directory)
7 {
8 IList<string> result = new List<string>();
9
10 foreach (FileInfo file in directory.GetFiles("*.dll"))
11 {
12 result.Add(file.FullName);
13 }
14
15 DirectoryInfo[] subDirectories = directory.GetDirectories();
16
17 foreach (DirectoryInfo subDirectory in subDirectories)
18 {
19 GetAllDllFileFromDir(subDirectory);
20 }
21
22 return result;
23 }
IList<string> dllFiles = permission.core.Api.GetAllDllFileFromDir(new DirectoryInfo(@"F:\test"));