Пример #1
0
        /// <summary>
        /// Checks if new update is available.
        /// </summary>
        /// <returns>returns true if new new update is available.</returns>
        private static bool CheckForUpdates()
        {
            if (string.IsNullOrWhiteSpace(AppConfig.UpdateUrl))
            {
                return(false);
            }

            var errorText = string.Empty;

            try
            {
                //var randUrl = AppConfig.UpdateUrl + (AppConfig.UpdateUrl.Contains('?') ? "&" : "?") + "rand=" + Guid.NewGuid().ToString();
                //var xml = Ext.ReadFile() Ext.DownloadString(randUrl, AppConfig.Proxy, AppConfig.ProxyUrl, AppConfig.ProxyUserName, AppConfig.ProxyPassword);
                var xml = Ext.ReadUpdateUrlFile();
                if (_debug)
                {
                    ShowDebugForm(AppConfig.UpdateUrl, xml);
                }

                errorText  = "Error in xml file structure which was downloaded from server.\n";
                _xmlUpdate = Ext.DeserializeXml <XmlUpdate>(xml);

                foreach (var file in _xmlUpdate.Files)
                {
                    var tmpFile = Path.Combine(AppConfig.AppExeFolder, file.File);
                    if (!File.Exists(tmpFile))
                    {
                        return(true);
                    }

                    var hash = Ext.MD5HexFile(tmpFile);
                    if (file.Hash != hash)
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("{0}{1}\nUrl: {2}\n", errorText, ex.Message, AppConfig.UpdateUrl), ex);
            }

            return(false);
        }
Пример #2
0
        /// <summary>
        /// Checks if new update is available.
        /// </summary>
        /// <returns>returns true if new new update is available.</returns>
        private static bool CheckForNewUpdater()
        {
            if (string.IsNullOrWhiteSpace(AppConfig.UpdaterUrl) || string.IsNullOrWhiteSpace(Ext.ExeMD5Hex))
            {
                return(false);
            }
            var errorText = string.Empty;

            try
            {
                var xml = Ext.ReadUpdaterUrlFile();
                if (_debug)
                {
                    ShowDebugForm(AppConfig.UpdaterUrl, xml);
                }

                errorText  = "Error in xml file structure which was downloaded from server.\n";
                _xmlUpdate = Ext.DeserializeXml <XmlUpdate>(xml);

                var updaterFile = _xmlUpdate.Files.FirstOrDefault(x => x.File == _xmlUpdate.AppExeName);
                return(updaterFile != null && updaterFile.Hash != Ext.ExeMD5Hex);


                //foreach (var file in XmlUpdate.Files)
                //{
                //    var tmpFile = Path.Combine(AppConfig.AppExeFolder, file.File);
                //    if (!File.Exists(tmpFile))
                //        return true;

                //    var hash = Ext.MD5HexFile(tmpFile);
                //    if (file.Hash != hash)
                //        return true;
                //}
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("{0}{1}\nUrl: {2}\n", errorText, ex.Message, AppConfig.UpdaterUrl), ex);
            }
        }
Пример #3
0
        private void bsDeploy_DoWork(object sender, DoWorkEventArgs e)
        {
            var updateDir      = txtDirectoryUpdate.Text.Trim();
            var destinationDir = txtDirectoryDestination.Text.Trim();
            var xmlFile        = Path.Combine(destinationDir, "update.txt");

            var xml = (XmlUpdate)e.Argument;

            //add files into xml
            foreach (var item in _fileInfo.Where(x => x.Checked))
            {
                xml.Files.Add(new XmlUpdate.XmlFile(item.Path, item.Path.Substring(updateDir.Length + 1), item.Hash));
            }
            var compressDir = Path.Combine(destinationDir, xml.CompressFolderName);

            if (!Directory.Exists(compressDir))
            {
                Directory.CreateDirectory(compressDir);
            }

            var deleteFiles = Directory.GetFiles(destinationDir).ToList();

            if (deleteFiles.Contains(xmlFile))
            {
                deleteFiles.Remove(xmlFile);
            }
            var deleteCompressedFiles = Directory.GetFiles(compressDir);

            var comparer = new PropertyComparer <XmlUpdate.XmlFile>("Hash");
            var distinct = xml.Files.Distinct(comparer).ToList();

            decimal max = deleteFiles.Count + deleteCompressedFiles.Length + distinct.Count;


            var oldCompressEqualsCurrent = false;

            if (File.Exists(xmlFile))
            {
                XmlUpdate oldXml = null;//Creating old serialized XmlUpdate from xml file
                try { oldXml = Ext.DeserializeXml <XmlUpdate>(File.ReadAllBytes(xmlFile)); }
                catch { }
                oldCompressEqualsCurrent = oldXml != null && oldXml.Compress == xml.Compress;
            }

            for (int i = 0; i < deleteFiles.Count; i++)
            {
                bsDeploy.ReportProgress((int)((1m + i) * progressFiles.Maximum / max), "Deleting: " + Path.GetFileName(deleteFiles[i]));
                File.Delete(deleteFiles[i]);
            }

            for (int i = 0; i < deleteCompressedFiles.Length; i++)
            {
                var file = Path.GetFileName(deleteCompressedFiles[i]);
                //if old compressed file equals current then just skip.
                if (oldCompressEqualsCurrent && distinct.Any(x => x.Hash + xml.Extension == file))
                {
                    continue;
                }

                bsDeploy.ReportProgress((int)((1m + deleteFiles.Count + i) * progressFiles.Maximum / max), "Deleting: " + file);
                File.Delete(deleteCompressedFiles[i]);
            }


            for (int i = 0; i < distinct.Count; i++)
            {
                var item = distinct[i];
                var file = Path.Combine(compressDir, item.Hash + xml.Extension);
                if (File.Exists(file))
                {
                    continue;                   //if old compressed file already exists then skip
                }
                bsDeploy.ReportProgress((int)((1 + deleteFiles.Count + deleteCompressedFiles.Length + i) * progressFiles.Maximum / max), "Compressing: " + Path.GetFileName(item.Path) + "  To: " + item.Hash + xml.Extension);
                switch (xml.Compress.ToLowerInvariant())
                {
                case "":
                    File.Copy(item.Path, file);
                    break;

                case "gzip":
                    GZipHelper.CompressFile(item.Path, file);
                    break;

                //case "zip":
                //    SharpZLibHelper.CompressFile(item.Path, Path.Combine(compressFolder, item.Hash + xml.Extension));
                //    progressFiles.PerformStep();
                //    break;

                case "7zip":
                    SevenZipHelper.CompressFileLZMA(item.Path, file);
                    break;

                default:
                    throw new ArgumentException("Invalid compress type (use: gzip, 7zip).");
                }
            }
            File.WriteAllText(xmlFile, Ext.SerializeXml(xml));
        }