Esempio n. 1
0
        /// <summary>
        /// 下载方法
        /// </summary>
        private UpdateWork DownLoad()
        {
            //比如uri=http://localhost/Rabom/1.rar;iis就需要自己配置了。
            //截取文件名
            //构造文件完全限定名,准备将网络流下载为本地文件

            using (WebClient web = new WebClient())
            {
                foreach (var item in UpdateVerList)
                {
                    try
                    {
                        LogTool.AddLog("更新程序:下载更新包文件" + item.ReleaseVersion);
                        web.DownloadFile(item.ReleaseUrl, tempPath + item.ReleaseVersion + ".zip");
                        OnUpdateProgess?.Invoke(60 / UpdateVerList.Count);
                    }
                    catch (Exception ex)
                    {
                        LogTool.AddLog("更新程序:更新包文件" + item.ReleaseVersion + "下载失败,本次停止更新,异常信息:" + ex.Message);
                        throw ex;
                    }
                }
                return(this);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 备份当前的程序目录信息
        /// </summary>
        private UpdateWork Bak()
        {
            try
            {
                LogTool.AddLog("更新程序:准备执行备份操作");

                DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
                foreach (var item in di.GetFiles())
                {
                    if (item.Name != mainName)//当前文件不需要备份
                    {
                        File.Copy(item.FullName, bakPath + item.Name, true);
                    }
                }
                //文件夹复制
                foreach (var item in di.GetDirectories())
                {
                    if (item.Name != "bak" && item.Name != "temp")
                    {
                        CopyDirectory(item.FullName, bakPath);
                    }
                }
                LogTool.AddLog("更新程序:备份操作执行完成,开始关闭应用程序");
                OnUpdateProgess?.Invoke(20);
                return(this);
            }
            catch (Exception EX)
            {
                throw EX;
            }
        }
Esempio n. 3
0
 private UpdateWork Start()
 {
     String[] StartInfo = UpdateVerList[UpdateVerList.Count - 1].ApplicationStart.Split(',');
     if (StartInfo.Length > 0)
     {
         foreach (var item in StartInfo)
         {
             LogTool.AddLog("更新程序:启动" + item);
             Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, item));
         }
     }
     OnUpdateProgess?.Invoke(100);
     return(this);
 }
Esempio n. 4
0
 private UpdateWork Update()
 {
     foreach (var item in UpdateVerList)
     {
         try
         {
             //如果是覆盖安装的话,先删除原先的所有程序
             if (item.UpdateMode == "Cover")
             {
                 DelLocal();
             }
             string path = tempPath + item.ReleaseVersion + ".zip";
             using (ZipFile zip = new ZipFile(path))
             {
                 LogTool.AddLog("更新程序:解压" + item.ReleaseVersion + ".zip");
                 zip.ExtractAll(AppDomain.CurrentDomain.BaseDirectory, ExtractExistingFileAction.OverwriteSilently);
                 LogTool.AddLog("更新程序:" + item.ReleaseVersion + ".zip" + "解压完成");
                 ExecuteINI();//执行注册表等更新以及删除文件
             }
             localInfo.LastUdpate      = item.ReleaseDate;
             localInfo.LocalVersion    = item.ReleaseVersion;
             localInfo.ServerUpdateUrl = item.ServerUpdateUrl;
             localInfo.SaveXml();
         }
         catch (Exception ex)
         {
             LogTool.AddLog("更新程序出现异常:异常信息:" + ex.Message);
             LogTool.AddLog("更新程序:更新失败,进行回滚操作");
             Restore();
             break;
         }
         finally
         {
             //删除下载的临时文件
             LogTool.AddLog("更新程序:删除临时文件" + item.ReleaseVersion);
             DelTempFile(item.ReleaseVersion + ".zip");//删除更新包
             LogTool.AddLog("更新程序:临时文件删除完成" + item.ReleaseVersion);
         }
     }
     OnUpdateProgess?.Invoke(98);
     return(this);
 }
Esempio n. 5
0
        /// <summary>
        /// 文件拷贝
        /// </summary>
        /// <param name="srcdir">源目录</param>
        /// <param name="desdir">目标目录</param>
        private UpdateWork CopyDirectory(string srcdir, string desdir)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);

            LogTool.AddLog(folderName + "SNDKSDDDDDDDDDD");

            string desfolderdir = desdir + "\\" + folderName;

            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);
            foreach (string file in filenames) // 遍历所有的文件和目录
            {
                if (Directory.Exists(file))    // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                {
                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }
                    CopyDirectory(file, desfolderdir);
                }
                else // 否则直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);
                    srcfileName = desfolderdir + "\\" + srcfileName;
                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }
                    File.Copy(file, srcfileName, true);
                }
            }
            return(this);
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            if (f)
            {
                try
                {
                    string programName   = args[0];
                    string silentUpdate  = args[1];
                    string isClickUpdate = "0";
                    string localAddress  = AppDomain.CurrentDomain.BaseDirectory;
                    if (args.Length == 3)
                    {
                        isClickUpdate = args[2];
                    }
                    if (string.IsNullOrEmpty(programName) == false)
                    {
                        LogTool.AddLog("--------------------11111111111111");

                        LogTool.AddLog($"{programName},{localAddress},{isClickUpdate}");

                        UpdateWork updateWork = new UpdateWork(programName, localAddress, isClickUpdate);

                        if (updateWork.UpdateVerList.Count > 0)
                        {
                            /* 当前用户是管理员的时候,直接启动应用程序
                             * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
                             */
                            //获得当前登录的Windows用户标示
                            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
                            //创建Windows用户主题
                            Application.EnableVisualStyles();
                            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
                            //判断当前登录用户是否为管理员
                            if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
                            {
                                if (silentUpdate == "1")
                                {
                                    updateWork.Do();
                                }
                                else
                                {
                                    Application.EnableVisualStyles();
                                    Application.SetCompatibleTextRenderingDefault(false);
                                    Application.Run(new MainForm(updateWork));
                                }
                            }
                            else
                            {
                                string result = Environment.GetEnvironmentVariable("systemdrive");
                                if (AppDomain.CurrentDomain.BaseDirectory.Contains(result))
                                {
                                    //创建启动对象
                                    ProcessStartInfo startInfo = new ProcessStartInfo
                                    {
                                        //设置运行文件
                                        FileName = System.Windows.Forms.Application.ExecutablePath,
                                        //设置启动动作,确保以管理员身份运行
                                        Verb = "runas",

                                        Arguments = " " + programName + " " + silentUpdate
                                    };
                                    //如果不是管理员,则启动UAC
                                    System.Diagnostics.Process.Start(startInfo);
                                }
                                else
                                {
                                    if (silentUpdate == "1")
                                    {
                                        updateWork.Do();
                                    }
                                    else
                                    {
                                        Application.EnableVisualStyles();
                                        Application.SetCompatibleTextRenderingDefault(false);
                                        Application.Run(new MainForm(updateWork));
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogTool.AddLog(ex.Message);
                    MessageBox.Show(ex.Message);
                }
            }
        }