/// <summary>加载插件</summary> /// <param name="typeName"></param> /// <param name="disname"></param> /// <param name="dll"></param> /// <param name="linkName"></param> /// <param name="url"></param> /// <returns></returns> public static Type LoadPlugin(String typeName, String disname, String dll, String linkName, String url) { var type = typeName.GetTypeEx(true); if (type != null) return type; if (dll.IsNullOrEmpty()) return null; // 先检查当前目录,再检查插件目录 var file = dll.GetFullPath(); if (!File.Exists(file) && Runtime.IsWeb) file = "Bin".GetFullPath().CombinePath(dll); if (!File.Exists(file)) file = Setting.Current.GetPluginPath().CombinePath(dll); // 如果本地没有数据库,则从网络下载 if (!File.Exists(file)) { XTrace.WriteLine("{0}不存在或平台版本不正确,准备联网获取 {1}", disname ?? dll, url); var client = new WebClientX(true, true); client.Log = XTrace.Log; var dir = Path.GetDirectoryName(file); var file2 = client.DownloadLinkAndExtract(url, linkName, dir); } if (!File.Exists(file)) { XTrace.WriteLine("未找到 {0} {1}", disname, dll); return null; } type = typeName.GetTypeEx(true); if (type != null) return type; //var assembly = Assembly.LoadFrom(file); //if (assembly == null) return null; //type = assembly.GetType(typeName); //if (type == null) type = AssemblyX.Create(assembly).GetType(typeName); return type; }
/// <summary>读取资源,并写入到文件</summary> /// <param name="name">名称</param> /// <param name="fileName"></param> public static void ReleaseFile(String name, String fileName) { fileName = fileName.GetFullPath(); if (String.IsNullOrEmpty(fileName) || File.Exists(fileName)) return; try { var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name); if (stream == null) return; var buffer = new Byte[stream.Length]; var count = stream.Read(buffer, 0, buffer.Length); fileName = fileName.GetFullPath(); var p = Path.GetDirectoryName(fileName); if (!String.IsNullOrEmpty(p) && !Directory.Exists(p)) Directory.CreateDirectory(p); File.WriteAllBytes(fileName, buffer); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
protected static void CheckAndDownload(String file, String targetPath = null) { if (!Path.IsPathRooted(file)) { if (!Runtime.IsWeb) file = file.GetFullPath(); else file = Path.Combine(HttpRuntime.BinDirectory, file); } if (File.Exists(file) && new FileInfo(file).Length > 0) return; // 目标目录 String dir = !String.IsNullOrEmpty(targetPath) ? targetPath : Path.GetDirectoryName(file); // 从网上下载文件 var zipfile = Path.GetFileNameWithoutExtension(file); try { #region 检测64位平台 var module = typeof(Object).Module; PortableExecutableKinds kind; ImageFileMachine machine; module.GetPEKind(out kind, out machine); if (machine != ImageFileMachine.I386) zipfile += "64"; #endregion zipfile += ".zip"; var url = String.Format(ServiceAddress, zipfile); var sw = new Stopwatch(); sw.Start(); // 检查缓存文件,一个月内有效 var x = Path.Combine(Path.GetPathRoot(Environment.SystemDirectory), @"X"); var xfile = Path.Combine(x, zipfile); var xf = new FileInfo(xfile); if (CacheZip && File.Exists(xfile) && xf.Length > 0 && xf.LastWriteTime.AddMonths(1) > DateTime.Now) zipfile = xfile; else { // 目标Zip文件 zipfile = Path.Combine(dir, zipfile); // Zip文件不存在,准备下载 if (!File.Exists(zipfile) || new FileInfo(zipfile).Length <= 0) { DAL.WriteLog("准备从{0}下载文件到{1}!", url, zipfile); var client = new WebClientX(true, true); // 同步下载,3秒超时 client.Timeout = 10000; //var data = client.DownloadData(url); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); client.DownloadFile(url, zipfile); client.Dispose(); } if (CacheZip) File.Copy(zipfile, xfile, true); } sw.Stop(); var size = new FileInfo(zipfile).Length; DAL.WriteLog("下载{0}完成({3:n0}字节),耗时{2},准备解压到{1}!", zipfile, dir, sw.Elapsed, size); //var ms = new MemoryStream(data); //if (file.EndsWith("64")) file = file.Substring(0, file.Length - 2); //IOHelper.DecompressFile(ms, dir, file, false); ZipFile.Extract(zipfile, dir, true); DAL.WriteLog("解压完成!"); } catch (ZipException ex) { if (File.Exists(zipfile)) File.Delete(zipfile); DAL.WriteLog("解压失败,删除压缩文件!{0}", ex.ToString()); } catch (Exception ex) { DAL.WriteLog(ex.ToString()); } }
/// <summary>备份文件到目标文件</summary> /// <param name="bakfile"></param> public void Backup(String bakfile) { bakfile = bakfile.GetFullPath().EnsureDirectory(); WriteLog("{0}备份SQLite数据库{1}到{2}", Database.ConnName, (Database as SQLite).FileName, bakfile); var sw = new Stopwatch(); sw.Start(); // 删除已有文件 if (File.Exists(bakfile)) File.Delete(bakfile); using (var session = Database.CreateSession()) using (var conn = Database.Factory.CreateConnection()) { session.Open(); conn.ConnectionString = "Data Source={0}".F(bakfile); conn.Open(); //var method = conn.GetType().GetMethodEx("BackupDatabase"); // 借助BackupDatabase函数可以实现任意两个SQLite之间倒数据,包括内存数据库 session.Conn.Invoke("BackupDatabase", conn, "main", "main", -1, null, 0); } // 压缩 WriteLog("备份文件大小:{0:n0}字节", bakfile.AsFile().Length); if (bakfile.EndsWithIgnoreCase(".zip")) { //var rnd = new Random(); var tmp = Path.GetDirectoryName(bakfile).CombinePath(Rand.Next() + ".tmp"); File.Move(bakfile, tmp); ZipFile.CompressFile(tmp, bakfile); File.Delete(tmp); WriteLog("压缩后大小:{0:n0}字节", bakfile.AsFile().Length); } sw.Stop(); WriteLog("备份完成,耗时{0:n0}ms", sw.ElapsedMilliseconds); }