コード例 #1
0
ファイル: DownloaderForm.cs プロジェクト: zhiyuanjia/ShareX
        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.CreateDirectoryFromDirectoryPath(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.ExceptionThrowed  += (v1, v2) => ChangeStatus(fileDownloader.LastException.Message);
                fileDownloader.StartDownload();

                ChangeStatus(Resources.DownloaderForm_StartDownload_Getting_file_size_);
            }
        }
コード例 #2
0
ファイル: SettingsBase.cs プロジェクト: wdichler/ShareX
        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();
                                    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}");
                        }

                        string backupFilePath = null;

                        if (CreateBackup)
                        {
                            string fileName = Path.GetFileName(filePath);
                            backupFilePath = Path.Combine(BackupFolder, fileName);
                            Helpers.CreateDirectoryFromDirectoryPath(BackupFolder);
                        }

                        File.Replace(tempFilePath, filePath, backupFilePath);

                        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);
        }