private void StartDownload() { if (!string.IsNullOrEmpty(URL) && Status == DownloaderFormStatus.Waiting) { Status = DownloaderFormStatus.DownloadStarted; btnAction.Text = Resources.DownloaderForm_StartDownload_Cancel; string folderPath = Path.Combine(Path.GetTempPath(), "ShareX"); Helpers.CreateDirectory(folderPath); DownloadLocation = Path.Combine(folderPath, Filename); fileDownloader = new FileDownloader(URL, DownloadLocation, Proxy, AcceptHeader); fileDownloader.FileSizeReceived += (v1, v2) => ChangeProgress(); fileDownloader.DownloadStarted += (v1, v2) => ChangeStatus(Resources.DownloaderForm_StartDownload_Downloading_); fileDownloader.ProgressChanged += (v1, v2) => ChangeProgress(); fileDownloader.DownloadCompleted += fileDownloader_DownloadCompleted; fileDownloader.ExceptionThrown += (v1, v2) => ChangeStatus(fileDownloader.LastException.Message); fileDownloader.StartDownload(); ChangeStatus(Resources.DownloaderForm_StartDownload_Getting_file_size_); } }
private bool SaveInternal(string filePath) { string typeName = GetType().Name; DebugHelper.WriteLine($"{typeName} save started: {filePath}"); bool isSuccess = false; try { if (!string.IsNullOrEmpty(filePath)) { lock (this) { Helpers.CreateDirectoryFromFilePath(filePath); string tempFilePath = filePath + ".temp"; using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough)) { SaveToStream(fileStream, SupportDPAPIEncryption); } if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath)) { throw new Exception($"{typeName} file is corrupt: {tempFilePath}"); } if (File.Exists(filePath)) { string backupFilePath = null; if (CreateBackup) { string fileName = Path.GetFileName(filePath); backupFilePath = Path.Combine(BackupFolder, fileName); Helpers.CreateDirectory(BackupFolder); } File.Replace(tempFilePath, filePath, backupFilePath); } else { File.Move(tempFilePath, filePath); } if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder)) { Helpers.BackupFileWeekly(filePath, BackupFolder); } isSuccess = true; } } } catch (Exception e) { DebugHelper.WriteException(e); OnSettingsSaveFailed(e); } finally { string status = isSuccess ? "successful" : "failed"; DebugHelper.WriteLine($"{typeName} save {status}: {filePath}"); } return(isSuccess); }
private bool SaveInternal(string filePath) { string typeName = GetType().Name; DebugHelper.WriteLine($"{typeName} save started: {filePath}"); bool isSuccess = false; try { if (!string.IsNullOrEmpty(filePath)) { lock (this) { Helpers.CreateDirectoryFromFilePath(filePath); string tempFilePath = filePath + ".temp"; using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter)) { JsonSerializer serializer = new JsonSerializer(); if (SupportDPAPIEncryption) { serializer.ContractResolver = new DPAPIEncryptedStringPropertyResolver(); } else { serializer.ContractResolver = new WritablePropertiesOnlyResolver(); } serializer.Converters.Add(new StringEnumConverter()); serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc; serializer.Formatting = Formatting.Indented; serializer.Serialize(jsonWriter, this); jsonWriter.Flush(); } if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath)) { throw new Exception($"{typeName} file is corrupt: {tempFilePath}"); } if (File.Exists(filePath)) { string backupFilePath = null; if (CreateBackup) { string fileName = Path.GetFileName(filePath); backupFilePath = Path.Combine(BackupFolder, fileName); Helpers.CreateDirectory(BackupFolder); } File.Replace(tempFilePath, filePath, backupFilePath); } else { File.Move(tempFilePath, filePath); } if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder)) { Helpers.BackupFileWeekly(filePath, BackupFolder); } isSuccess = true; } } } catch (Exception e) { DebugHelper.WriteException(e); OnSettingsSaveFailed(e); } finally { string status = isSuccess ? "successful" : "failed"; DebugHelper.WriteLine($"{typeName} save {status}: {filePath}"); } return(isSuccess); }