private static bool SaveInternal(object obj, string filePath, bool createBackup) { string typeName = obj.GetType().Name; DebugHelper.WriteLine("{0} save started: {1}", typeName, filePath); bool isSuccess = false; try { if (!string.IsNullOrEmpty(filePath)) { lock (obj) { Helpers.CreateDirectoryIfNotExist(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)) { jsonWriter.Formatting = Formatting.Indented; JsonSerializer serializer = new JsonSerializer(); serializer.ContractResolver = new WritablePropertiesOnlyResolver(); serializer.Converters.Add(new StringEnumConverter()); serializer.Serialize(jsonWriter, obj); jsonWriter.Flush(); } if (File.Exists(filePath)) { if (createBackup) { File.Copy(filePath, filePath + ".bak", true); } File.Delete(filePath); } File.Move(tempFilePath, filePath); isSuccess = true; } } } catch (Exception e) { DebugHelper.WriteException(e); } finally { DebugHelper.WriteLine("{0} save {1}: {2}", typeName, isSuccess ? "successful" : "failed", filePath); } return(isSuccess); }
public static bool WriteToFile(this Stream stream, string filePath) { if (stream.Length > 0 && !string.IsNullOrEmpty(filePath)) { Helpers.CreateDirectoryIfNotExist(filePath); using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { stream.CopyStreamTo(fileStream); } return(true); } return(false); }
public void SaveLog(string filepath) { lock (loggerLock) { if (sbMessages != null && sbMessages.Length > 0 && !string.IsNullOrEmpty(filepath)) { string messages = sbMessages.ToString(lastSaveIndex, sbMessages.Length - lastSaveIndex); if (!string.IsNullOrEmpty(messages)) { Helpers.CreateDirectoryIfNotExist(filepath); File.AppendAllText(filepath, messages, Encoding.UTF8); lastSaveIndex = sbMessages.Length; } } } }
public static bool ExtractFFmpeg(string zipPath, string extractPath) { try { if (NativeMethods.Is64Bit()) { SevenZipExtractor.SetLibraryPath(Path.Combine(Application.StartupPath, "7z-x64.dll")); } else { SevenZipExtractor.SetLibraryPath(Path.Combine(Application.StartupPath, "7z.dll")); } Helpers.CreateDirectoryIfNotExist(extractPath); using (SevenZipExtractor zip = new SevenZipExtractor(zipPath)) { Regex regex = new Regex(@"^ffmpeg-.+\\bin\\ffmpeg\.exe$", RegexOptions.Compiled | RegexOptions.CultureInvariant); foreach (ArchiveFileInfo item in zip.ArchiveFileData) { if (regex.IsMatch(item.FileName)) { using (FileStream fs = new FileStream(extractPath, FileMode.Create, FileAccess.Write, FileShare.None)) { zip.ExtractFile(item.Index, fs); return(true); } } } } } catch (Exception e) { DebugHelper.WriteException(e); } return(false); }
/// <param name="sourceFilePath">AVI file path</param> /// <param name="targetFilePath">Target file path without extension</param> public void Encode(string sourceFilePath, string targetFilePath) { if (IsValid() && !string.IsNullOrEmpty(sourceFilePath) && !string.IsNullOrEmpty(targetFilePath)) { if (!targetFilePath.EndsWith(OutputExtension)) { targetFilePath += "." + OutputExtension.TrimStart('.'); } Helpers.CreateDirectoryIfNotExist(targetFilePath); using (Process process = new Process()) { ProcessStartInfo psi = new ProcessStartInfo(Path); psi.Arguments = Args.Replace("%input", "\"" + sourceFilePath + "\"").Replace("%output", "\"" + targetFilePath + "\""); psi.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo = psi; process.Start(); process.WaitForExit(); } } }
public Logger(string logFilePath) : this() { LogFilePath = logFilePath; Helpers.CreateDirectoryIfNotExist(LogFilePath); }
public Logger(string logFilePath) { FileWrite = true; LogFilePath = logFilePath; Helpers.CreateDirectoryIfNotExist(LogFilePath); }