Exemplo n.º 1
0
        /// <summary>
        /// 手动销毁当前项
        /// </summary>
        public void Destroyed()
        {
            try
            {
                if (timDownSpeedTask != null)
                {
                    //主时钟,倒计时,移除下载事件
                    timDownSpeedTask.Stop();
                    timDownSpeedTask.Enabled = false;
                    timDownSpeedTask.Tick   -= timDownSpeedTask_Tick;
                    timDownSpeedTask         = null;
                }

                if (webcDownloading != null)
                {
                    //移除下载进度改变事件
                    webcDownloading.DownloadProgressChanged -= DownloadProgressChanged;
                    webcDownloading.DownloadFileCompleted   -= DownloadFileCompleted;
                }

                if (webcDownloading != null)
                {
                    webcDownloading.Dispose();
                    webcDownloading = null;
                }
                bolItemDestroyed = true;
            }
            catch (Exception ex)
            {
            }
        }
Exemplo n.º 2
0
        /// <summary>子类重载实现资源释放逻辑时必须首先调用基类方法</summary>
        /// <param name="disposing">从Dispose调用(释放所有资源)还是析构函数调用(释放非托管资源)</param>
        protected override void OnDispose(bool disposing)
        {
            base.OnDispose(disposing);

            if (_Client != null)
            {
                _Client.Dispose();
            }

            if (hasLogined)
            {
                Logout();
            }
        }
Exemplo n.º 3
0
        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());
            }
        }