Exemple #1
0
        /// <summary>
        /// 安装/升级插件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
        public static bool InstallPlugin(string url, PluginHandler <PluginPackAttribute> handler)
        {
            var    installResult = false;
            string appDirectory  = AppDomain.CurrentDomain.BaseDirectory;
            var    pluginPath    = String.Concat(
                appDirectory,
                PluginConfig.PLUGIN_DIRECTORY);

            var tempDir  = String.Concat(appDirectory, PluginConfig.PLUGIN_TMP_DIRECTORY, "plugin/");
            var fileName = tempDir + "dl_pack_" + String.Empty.RandomLetters(16) + ".zip";

            if (Directory.Exists(tempDir))
            {
                Directory.Delete(tempDir, true);
            }
            Directory.CreateDirectory(tempDir).Create();

            var dir = new DirectoryInfo(tempDir);

            var data = HttpClient.DownloadFile(url, null);

            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                fs.Write(data, 0, data.Length);
                fs.Flush();
                fs.Dispose();
            }

            IList <PluginPackAttribute> pluginAttrs = new List <PluginPackAttribute>(GetPluginPackInfo(fileName));

            if (pluginAttrs.Count != 0)
            {
                ZipUtility.UncompressFile(pluginPath, fileName, false);

                string[] multExt = PluginConfig.GetFilePartterns();
                foreach (String ext in multExt)
                {
                    foreach (FileInfo file in dir.GetFiles(ext))
                    {
                        File.Delete(pluginPath + file.Name);
                        file.MoveTo(pluginPath + file.Name);
                    }
                }


                if (handler != null)
                {
                    var result = false;
                    foreach (PluginPackAttribute attr in pluginAttrs)
                    {
                        handler(attr, ref result);
                    }
                }

                installResult = true;
            }

            Directory.Delete(tempDir, true);

            return(installResult);
        }