/// <summary>
        /// 加载程序集
        /// </summary>
        /// <param name="AssemblyPath">程序集路径</param>
        /// <param name="DynamicLoad">在内存中动态加载</param>
        /// <returns>程序集</returns>
        public static Assembly CreateAssembly(string AssemblyPath, bool DynamicLoad = true)
        {
            LogController.Info("开始{0}加载程序集路径:{1} ...", (DynamicLoad ? "动态" : string.Empty), AssemblyPath);

            Assembly PluginAssembly = null;

            try
            {
                if (!DynamicLoad)
                {
                    // 从链接库文件路径加载
                    PluginAssembly = Assembly.LoadFrom(AssemblyPath);
                }
                else
                {
                    // 把链接库文件读入内存后从内存加载,允许程序在运行时更新链接库
                    using (FileStream AssemblyStream = new FileStream(AssemblyPath, FileMode.Open, FileAccess.Read))
                    {
                        using (BinaryReader AssemblyReader = new BinaryReader(AssemblyStream))
                        {
                            byte[] AssemblyBuffer = AssemblyReader.ReadBytes((int)AssemblyStream.Length);
                            PluginAssembly = Assembly.Load(AssemblyBuffer);
                            AssemblyReader.Close();
                        }
                        AssemblyStream.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                LogController.Error("创建程序集遇到异常:{0}", ex.Message);
                return(null);
            }

            return(PluginAssembly);
        }
示例#2
0
        /// <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));
        }