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) { FileHelpers.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); FileHelpers.CreateDirectory(BackupFolder); } File.Replace(tempFilePath, filePath, backupFilePath); } else { File.Move(tempFilePath, filePath); } if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder)) { FileHelpers.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 static T LoadInternal(string filePath, List <string> fallbackFilePaths = null) { string typeName = typeof(T).Name; if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) { DebugHelper.WriteLine($"{typeName} load started: {filePath}"); try { using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (fileStream.Length > 0) { T settings; using (StreamReader streamReader = new StreamReader(fileStream)) using (JsonTextReader jsonReader = new JsonTextReader(streamReader)) { JsonSerializer serializer = new JsonSerializer(); serializer.ContractResolver = new DPAPIEncryptedStringPropertyResolver(); serializer.Converters.Add(new StringEnumConverter()); serializer.DateTimeZoneHandling = DateTimeZoneHandling.Local; serializer.ObjectCreationHandling = ObjectCreationHandling.Replace; serializer.Error += Serializer_Error; settings = serializer.Deserialize <T>(jsonReader); } if (settings == null) { throw new Exception($"{typeName} object is null."); } DebugHelper.WriteLine($"{typeName} load finished: {filePath}"); return(settings); } else { throw new Exception($"{typeName} file stream length is 0."); } } } catch (Exception e) { DebugHelper.WriteException(e, $"{typeName} load failed: {filePath}"); } } else { DebugHelper.WriteLine($"{typeName} file does not exist: {filePath}"); } if (fallbackFilePaths != null && fallbackFilePaths.Count > 0) { filePath = fallbackFilePaths[0]; fallbackFilePaths.RemoveAt(0); return(LoadInternal(filePath, fallbackFilePaths)); } DebugHelper.WriteLine($"Loading new {typeName} instance."); return(new T()); }
public string Run(string inputPath) { if (!string.IsNullOrEmpty(Path) && File.Exists(Path) && !string.IsNullOrWhiteSpace(inputPath)) { inputPath = inputPath.Trim('"'); if (CheckExtension(inputPath, Extensions)) { try { string outputPath = inputPath; string arguments; if (string.IsNullOrEmpty(Args)) { arguments = '"' + inputPath + '"'; } else { if (!string.IsNullOrWhiteSpace(OutputExtension)) { outputPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(inputPath), System.IO.Path.GetFileNameWithoutExtension(inputPath)); if (!OutputExtension.StartsWith(".")) { outputPath += "."; } outputPath += OutputExtension; } arguments = CodeMenuEntryActions.Parse(Args, inputPath, outputPath); } using (Process process = new Process()) { ProcessStartInfo psi = new ProcessStartInfo() { FileName = Path, Arguments = arguments, UseShellExecute = false, CreateNoWindow = HiddenWindow }; process.StartInfo = psi; DebugHelper.WriteLine($"CLI: \"{psi.FileName}\" {psi.Arguments}"); process.Start(); process.WaitForExit(); } if (!string.IsNullOrEmpty(outputPath) && File.Exists(outputPath)) { if (DeleteInputFile && !inputPath.Equals(outputPath, StringComparison.OrdinalIgnoreCase) && File.Exists(inputPath)) { DebugHelper.WriteLine("Deleting input file: " + inputPath); File.Delete(inputPath); } return(outputPath); } return(inputPath); } catch (Exception e) { DebugHelper.WriteException(e); } } } return(inputPath); }
public string Run(string filePath) { if (!string.IsNullOrEmpty(filePath) && CheckExtensions(filePath) && !string.IsNullOrEmpty(Path) && File.Exists(Path)) { filePath = filePath.Trim('"'); try { string newFilePath = ""; using (Process process = new Process()) { ProcessStartInfo psi = new ProcessStartInfo(Path); if (string.IsNullOrEmpty(Args)) { psi.Arguments = '"' + filePath + '"'; } else { string args = Args.Replace("%filepath%", '"' + filePath + '"').Replace("%input", '"' + filePath + '"'); if (!string.IsNullOrEmpty(OutputExtension)) { newFilePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(filePath), System.IO.Path.GetFileNameWithoutExtension(filePath)); if (!OutputExtension.Contains(".")) { OutputExtension = "." + OutputExtension; } newFilePath += OutputExtension; args = args.Replace("%output", '"' + newFilePath + '"'); } psi.Arguments = args; } process.StartInfo = psi; DebugHelper.WriteLine(string.Format("Running {0} with arguments: {1}", Path, psi.Arguments)); process.Start(); process.WaitForExit(); } if (!string.IsNullOrEmpty(newFilePath) && File.Exists(newFilePath)) { return(newFilePath); } return(filePath); } catch (Exception e) { DebugHelper.WriteException(e); } } return(filePath); }
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)) 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 (File.Exists(filePath)) { if (CreateBackup) { Helpers.CopyFile(filePath, BackupFolder); } File.Delete(filePath); } 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); }
public override void CheckUpdate() { try { List <GitHubRelease> releases = GetReleases(); if (releases != null && releases.Count > 0) { GitHubRelease latestRelease; if (IncludePreRelease) { latestRelease = releases[0]; } else { latestRelease = releases.FirstOrDefault(x => !x.prerelease); } if (latestRelease != null && !string.IsNullOrEmpty(latestRelease.tag_name) && latestRelease.tag_name.Length > 1 && latestRelease.tag_name[0] == 'v') { LatestVersion = new Version(latestRelease.tag_name.Substring(1)); if (latestRelease.assets != null && latestRelease.assets.Count > 0) { string extension; if (IsPortable) { extension = "portable.zip"; } else { extension = ".exe"; } foreach (GitHubAsset asset in latestRelease.assets) { if (asset != null && !string.IsNullOrEmpty(asset.name) && asset.name.EndsWith(extension, StringComparison.InvariantCultureIgnoreCase)) { Filename = asset.name; if (IsPortable) { DownloadURL = asset.browser_download_url; } else { DownloadURL = asset.url; } RefreshStatus(); return; } } } } } } catch (Exception e) { DebugHelper.WriteException(e, "GitHub update check failed"); } Status = UpdateStatus.UpdateCheckFailed; }
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.CreateDirectoryFromDirectoryPath(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); }