Ejemplo n.º 1
0
        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.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))
                                {
                                    jsonWriter.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
                                    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);
        }
Ejemplo n.º 2
0
        public static void SerializeToFile <T>(T obj, string filePath, DefaultValueHandling defaultValueHandling = DefaultValueHandling.Include,
                                               NullValueHandling nullValueHandling = NullValueHandling.Include, ISerializationBinder serializationBinder = null)
        {
            if (!string.IsNullOrEmpty(filePath))
            {
                Helpers.CreateDirectoryFromFilePath(filePath);

                using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
                {
                    SerializeToStream(obj, fileStream, defaultValueHandling, nullValueHandling, serializationBinder);
                }
            }
        }
Ejemplo n.º 3
0
        public static bool WriteToFile(this Stream stream, string filePath)
        {
            if (stream.Length > 0 && !string.IsNullOrEmpty(filePath))
            {
                Helpers.CreateDirectoryFromFilePath(filePath);

                using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    stream.CopyStreamTo(fileStream);
                }

                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
        public static void Compress(string archivePath, List <ZipEntryInfo> entries, CompressionLevel compression = CompressionLevel.Optimal)
        {
            Helpers.CreateDirectoryFromFilePath(archivePath);

            if (File.Exists(archivePath))
            {
                File.Delete(archivePath);
            }

            using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create))
            {
                foreach (ZipEntryInfo entry in entries)
                {
                    archive.CreateEntry(entry, compression);
                }
            }
        }
Ejemplo n.º 5
0
        /// <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.CreateDirectoryFromFilePath(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();
                }
            }
        }
Ejemplo n.º 6
0
        public static bool ExtractFFmpeg(string zipPath, string extractPath)
        {
            try
            {
                if (NativeMethods.Is64Bit())
                {
                    SevenZipExtractor.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7z-x64.dll"));
                }
                else
                {
                    SevenZipExtractor.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7z.dll"));
                }

                Helpers.CreateDirectoryFromFilePath(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);
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
 public Logger(string logFilePath)
 {
     FileWrite   = true;
     LogFilePath = logFilePath;
     Helpers.CreateDirectoryFromFilePath(LogFilePath);
 }
Ejemplo n.º 10
0
        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);
        }