/// <summary> /// 扫描目录内所有 Program 插件; /// </summary> /// <param name="ProgramDirectory">插件目录</param> /// <param name="Extension">文件扩展名</param> /// <returns></returns> public static IEnumerable <ProgramTemplateClass> ScanProgramPlugins(string ProgramDirectory, string Extension = ".dll") { LogController.Debug("扫描 Program 插件 : {0}", ProgramDirectory); if (!Directory.Exists(ProgramDirectory)) { yield break; } foreach (string ProgramPath in Directory.GetFiles(ProgramDirectory).Where(ProgramPath => ProgramPath.ToLower().EndsWith(Extension))) { LogController.Debug("发现 Program 插件 dll 文件 : {0}", ProgramPath); Assembly ProgramAssembly = AssemblyController <ProgramTemplateClass> .CreateAssembly(ProgramPath); if (ProgramAssembly == null) { continue; } foreach (ProgramTemplateClass ProgramInstance in AssemblyController <ProgramTemplateClass> .CreateTypeInstance(ProgramAssembly)) { LogController.Info("创建 Program 实例 : {0} ({1})", ProgramInstance.Name, ProgramInstance.Description); yield return(ProgramInstance); } } }
/// <summary> /// 扫描目录内所有 StartUp 插件; /// </summary> /// <param name="StartUpDirectory">插件目录</param> /// <param name="Extension">文件扩展名</param> /// <returns></returns> public static IEnumerable <StartUpTemplateClass> ScanStartUpPlugins(string StartUpDirectory, string Extension = ".dll") { LogController.Debug("扫描 StartUp 插件 : {0}", StartUpDirectory); if (!Directory.Exists(StartUpDirectory)) { yield break; } foreach (string StartUpPath in Directory.GetFiles(StartUpDirectory).Where(StartUpPath => StartUpPath.ToLower().EndsWith(Extension))) { LogController.Debug("发现 StartUp 插件 dll 文件 : {0}", StartUpPath); Assembly StartUpAssembly = AssemblyController <StartUpTemplateClass> .CreateAssembly(StartUpPath); if (StartUpAssembly == null) { continue; } foreach (StartUpTemplateClass StartUpInstance in AssemblyController <StartUpTemplateClass> .CreateTypeInstance(StartUpAssembly)) { LogController.Info("创建 StartUp 实例 : {0} ({1})", StartUpInstance.Name, StartUpInstance.Description); yield return(StartUpInstance); } } }
/// <summary> /// 扫描目录内所有 Logon 插件; /// </summary> /// <param name="LogonDirectory">插件目录</param> /// <param name="Extension">文件扩展名</param> /// <returns></returns> public static IEnumerable <LogonTemplateClass> ScanLogonPlugins(string LogonDirectory, string Extension = ".dll") { LogController.Debug("扫描 Logon 插件 : {0}", LogonDirectory); if (!Directory.Exists(LogonDirectory)) { yield break; } foreach (string LogonPath in Directory.GetFiles(LogonDirectory).Where(LogonPath => LogonPath.ToLower().EndsWith(Extension))) { LogController.Debug("发现 Logon 插件 dll 文件 : {0}", LogonPath); Assembly LogonAssembly = AssemblyController <LogonTemplateClass> .CreateAssembly(LogonPath); if (LogonAssembly == null) { continue; } foreach (LogonTemplateClass LogonInstance in AssemblyController <LogonTemplateClass> .CreateTypeInstance(LogonAssembly)) { LogController.Info("创建 Logon 实例 : {0} ({1})", LogonInstance.Name, LogonInstance.Description); yield return(LogonInstance); } } }
/// <summary> /// 从程序集创建指定父类派生的所有子类的实例 /// </summary> /// <param name="PluginAssembly">程序集</param> /// <param name="TargetTypeName"></param> /// <returns>所有子类的实例</returns> public static IEnumerable <T> CreateTypeInstance(Assembly PluginAssembly, string TargetTypeName = "") { if (PluginAssembly == null) { yield break; } LogController.Info("在程序集 {0} 中创建所有 {1} 派生的子类实例 ...", PluginAssembly.FullName, typeof(T).ToString()); // 仅加载范式类型派生的子类 foreach (Type PluginType in PluginAssembly.GetTypes().Where( (PluginType => TargetTypeName == string.Empty ? PluginType.IsSubclassOf(typeof(T)) : PluginType.IsSubclassOf(typeof(T)) && PluginType.Name == TargetTypeName) )) { LogController.Debug("可加载的插件类型 : {0}", PluginType.FullName); //创建允许加载类型的实例 T PluginInstance = null; try { PluginInstance = PluginAssembly.CreateInstance(PluginType.FullName) as T; } catch (Exception ex) { LogController.Error("创建 {0} 类型实例遇到异常 : {1}", PluginType.FullName, ex.Message); continue; } yield return(PluginInstance); } }
/// <summary> /// 转换图像为 Base64 编码 /// </summary> /// <param name="ImageObject">图像</param> /// <param name="ImageFormatType">图像格式</param> /// <returns>Base64</returns> public static string ImageToBase64(Image ImageObject, ImageFormat ImageFormatType) { LogController.Debug("转换图像(HashCode = 0x{0})为 Base64 编码 ...", ImageObject.GetHashCode().ToString("X")); try { MemoryStream ImageStream = new MemoryStream(); ImageObject.Save(ImageStream, ImageFormatType); return(Convert.ToBase64String(ImageStream.GetBuffer())); } catch (Exception ex) { LogController.Error("转换图像为 Base64 编码时遇到错误:{0}", ex.Message); return(string.Empty); } }
/// <summary> /// 转换 Base64 编码为图像 /// </summary> /// <param name="Base64String">Base64</param> /// <returns>图像</returns> public static Image Base64ToImage(string Base64String) { LogController.Debug("转换 Base64 编码 (HashCode = 0x{0})为图像 ...", Base64String.GetHashCode().ToString("X")); try { byte[] ImageData = Convert.FromBase64String(Base64String); MemoryStream ImageStream = new MemoryStream(ImageData); return(Image.FromStream(ImageStream)); } catch (Exception ex) { LogController.Error("转换 Base64 编码为图像时遇到错误:{0}", ex.Message); return(null); } }
/// <summary> /// 复制文件 /// </summary> /// <param name="SourcesPath">源文件路径</param> /// <param name="TargetPath">目标文件路径</param> public static void CopyFile(string SourcesPath, string TargetPath, bool OverWrite, bool ThrowException) { LogController.Debug("复制文件 \"{0}\" 至 \"{1}\",{2}允许复写,{3}允许抛出异常 ", SourcesPath, TargetPath, OverWrite ? "" : "不", ThrowException ? "" : "不"); try { File.Copy(SourcesPath, TargetPath, OverWrite); } catch (Exception CopyException) { LogController.Error("复制文件遇到错误:{0}", CopyException.Message); if (ThrowException) { throw CopyException; } } }
/// <summary> /// 复制目录 /// </summary> /// <param name="SourceDirectory">源目录</param> /// <param name="TargetDirectory">目标目录</param> /// <param name="CopyChildDir">是否复制子目录</param> /// <returns>返回 (总目录数, 成功数, 总文件数, 成功数)</returns> public static Tuple <int, int, int, int> CopyDirectory(string SourceDirectory, string TargetDirectory, bool CopyChildDir) { LogController.Debug("复制目录:{0} => {1}", SourceDirectory, TargetDirectory); //依次对应返回值 int DirCount = 0, DirOKCount = 0, FileCount = 0, FileOKCount = 0; if (!Directory.Exists(SourceDirectory)) { LogController.Warn("源目录 {0} 不存在,无法复制", SourceDirectory); return(new Tuple <int, int, int, int>(0, 0, 0, 0)); } if (!Directory.Exists(TargetDirectory)) { try { Directory.CreateDirectory(TargetDirectory); } catch (Exception ex) { LogController.Error("创建目录 {0} 失败:{1}", TargetDirectory, ex.Message); return(new Tuple <int, int, int, int>(0, 0, 0, 0)); } } DirOKCount++; //复制文件 foreach (string FilePath in Directory.GetFiles(SourceDirectory)) { LogController.Debug("复制文件:{0}", FilePath); try { File.Copy(FilePath, PathCombine(TargetDirectory, Path.GetFileName(FilePath)), true); FileOKCount++; } catch (Exception ex) { LogController.Error("复制文件遇到错误:{0}", ex.Message); } finally { FileCount++; } } if (CopyChildDir) { //复制子目录 foreach (string ChildDirectoryPath in Directory.GetDirectories(SourceDirectory)) { DirCount++; Tuple <int, int, int, int> CopyResult = CopyDirectory(ChildDirectoryPath, PathCombine(TargetDirectory, Path.GetFileName(ChildDirectoryPath)), CopyChildDir); DirCount += CopyResult.Item1; DirOKCount += CopyResult.Item2; FileCount += CopyResult.Item3; FileOKCount += CopyResult.Item4; LogController.Debug("复制子目录:{0}\n\t\t\t\t\t\t\t DirCount:{1} DirOKCount:{2} FileCount:{3} FileOKCount:{4}", ChildDirectoryPath, CopyResult.Item1, CopyResult.Item2, CopyResult.Item3, CopyResult.Item4); } } DirOKCount++; return(new Tuple <int, int, int, int>(DirCount, DirOKCount, FileCount, FileOKCount)); }