private void btnDownload_Click(object sender, EventArgs e) { var client = new AgentSvcClient(); try { var stream = client.DownloadConfig(sessionID, new ConfigOptions()); if (stream == null) { MessageBox.Show(@"Download stream is null."); } else { var t0 = DateTime.UtcNow; var buf = new byte[1024]; using (var fileStream = File.Open(DefConfigArc, FileMode.Create, FileAccess.Write, FileShare.Read)) { stream.CopyTo(fileStream); } stream.Dispose(); MessageBox.Show(@"Done in " + (int)(DateTime.UtcNow - t0).TotalMilliseconds + @" ms"); } } finally { client.Close(); } }
/// <summary> /// Downloads the configuration to the file. /// </summary> public void DownloadConfig(string destFileName, ConfigOptions configOptions) { if (string.IsNullOrEmpty(destFileName)) { throw new ArgumentException("destFileName must not be empty.", "destFileName"); } if (configOptions == null) { throw new ArgumentNullException("configOptions"); } RestoreConnection(); if (IsLocal) { // copy the configuration locally if (!client.PackConfig(sessionID, destFileName, configOptions)) { throw new ScadaException(Localization.UseRussian ? "Не удалось упаковать конфигурацию в архив." : "Unable to pack the configuration in the archive."); } } else { // transfer the configuration over the network Stream downloadStream = client.DownloadConfig(sessionID, configOptions); if (downloadStream == null) { throw new ScadaException(Localization.UseRussian ? "Отсутствуют данные для скачивания." : "No data to download."); } try { using (FileStream destStream = new FileStream(destFileName, FileMode.Create, FileAccess.Write, FileShare.Read)) { downloadStream.CopyTo(destStream); } } finally { downloadStream.Dispose(); } } RegisterActivity(); }