public void ReadLocal() { FileInfo fileinfo = new FileInfo(_filename); if (!fileinfo.Exists) return; try { document.Load(_filename); document.Validate(new ValidationEventHandler(document_ValidationEventHandler)); XmlElement root = document.DocumentElement; XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable); nsmgr.AddNamespace("xs", SchemaTargetName); foreach (XmlNode product in root.ChildNodes) { var configItem = new Config(); configItem.Version = XmlConvert.ToInt32(product.Attributes["version"].Value); configItem.File = product.Attributes["file"].Value; configItem.ProductId = product.Attributes["name"].Value; localConfigs.Add(configItem); } } catch (XmlException e) { throw new Exception("Ошибка чтения файла конфигурации.", e); } }
public void SetLocal(Config config) { Config localConfig = localConfigs.Find(x => x.ProductId == config.ProductId); if (localConfig != null) { localConfig.Version = config.Version; } else localConfigs.Add(config); Save(); }
public string DownloadUpdate(Config config) { var remoteUri = new Uri(PrepareDownloadPath(String.Format(@"{0}/{1}", _settings.RemoteConfigPath, config.File))); string localPath = PrepareDownloadPath(String.Format(@"{0}/{1}", Path.GetTempPath(), config.File)); using (var client = new WebClient()) { using (var remoteStream = client.OpenRead(remoteUri)) { using (var fileStream = new FileStream(localPath, FileMode.Create)) { using (var reader = new BinaryReader(remoteStream)) { using (var writer = new BinaryWriter(fileStream)) { var buffer = reader.ReadBytes(BinaryReaderBufferSize); while (buffer.Length > 0) { writer.Write(buffer); buffer = reader.ReadBytes(BinaryReaderBufferSize); } return localPath; } } } } } }
public Config GetRemote() { var uri = new Uri(PrepareDownloadPath(String.Format(@"{0}/{1}", _settings.RemoteConfigPath, _settings.RemoteConfigName))); try { using (var client = new WebClient()) { using (Stream stream = client.OpenRead(uri)) { XmlDocument doc = new XmlDocument(); doc.Load(stream); XmlElement root = doc.DocumentElement; if (root == null) { throw new Exception("Не удалось найти информацию об обновлениях для продукта."); } XmlNode xmlConfig = root.ChildNodes[0]; Config config = new Config(); config.Version = XmlConvert.ToInt32(xmlConfig.Attributes["version"].Value); config.ProductId = xmlConfig.Attributes["name"].Value; config.File = xmlConfig.Attributes["file"].Value; return config; } } } catch (UriFormatException ex) { throw new System.Net.WebException(String.Format("Неправильный формат строки адреса для поиска обновлений '{0}'.", uri), ex); } catch (Exception ex) { throw new System.Net.WebException(String.Format("Не удалось прочитать файл с информацией об обновлении \"{0}\"\r\n({1}: {2}).", uri, ex.GetType().ToString(), ex.Message), ex); } }
private bool ApplyUpdate(string localUpdate, int currentVersion) { if (String.IsNullOrEmpty(localUpdate)) throw new Exception("Пакет обновления должен быть скачан перед применением"); var process = Process.Start(localUpdate); process.WaitForExit(); if (process.ExitCode == 0) { Config config = updater.GetLocal(); if (config == null) config = new Config() { ProductId = Settings.Default.ProductName }; config.Version = currentVersion; updater.SetLocal(config); return true; } return false; }