/// <summary> /// 在搜索程序集路径的时候所使用的方法 /// </summary> /// <param name="enumValue"></param> /// <param name="arg"></param> /// <returns></returns> private static bool EnumDir(ref string enumValue, EnumDirectoryArg arg) { string totalPath = Path.Combine(enumValue, arg.Tag.ToString()); if (File.Exists(totalPath)) { return(true); } return(false); }
/// <summary> /// 从程序集目录下搜索指定的文件 /// </summary> /// <param name="fileName">需要搜索的文件名,如果是全路径则直接返回</param> /// <returns></returns> public static string FindAssemblyFile(string fileName) { if (fileName == null || fileName == string.Empty) { return(null); } if (fileName.IndexOf(":") > 0) { return(fileName); } string domainPath = AppDomain.CurrentDomain.BaseDirectory; using (EnumDirectoryArg res = new EnumDirectoryArg()) { res.Tag = fileName; if (EnumDirectory(ref domainPath, new EnumDirectoryMethodHandler(EnumDir), null, res)) { return(Path.Combine(res.TargetPath, fileName)); } } string holePath; string[] pathes = Environment.GetEnvironmentVariable("Path").Split(';'); for (int i = 0; i < pathes.Length; i++) { pathes[i] = GetPath(pathes[i]); holePath = Path.Combine(pathes[i], fileName); if (File.Exists(holePath)) { return(holePath); } } return(null); }
/// <summary> /// 遍历指定文件夹的所有目录 /// </summary> /// <param name="dir">给定的文件夹</param> /// <param name="method">搜索时所要执行的方法</param> /// <param name="filter">文件名过滤器</param> /// <returns></returns> static bool EnumDirectory(ref string dir, EnumDirectoryMethodHandler method, string filter, EnumDirectoryArg result) { if (result != null) { result.Success = false; } if (dir == null || dir == string.Empty) { return(false); } if (method(ref dir, result)) { if (result != null) { result.Success = true; result.TargetPath = dir; } return(true); } string[] dirs = null; if (filter != null && filter != string.Empty) { dirs = Directory.GetDirectories(dir, filter); } else { dirs = Directory.GetDirectories(dir); } for (int i = 0; i < dirs.Length; i++) { if (method(ref dirs[i], result)) { if (result != null) { result.Success = true; result.TargetPath = dirs[i]; } return(true); } } for (int i = 0; i < dirs.Length; i++) { if (EnumDirectory(ref dirs[i], method, filter, result)) { return(true); } } return(false); }