예제 #1
0
        public static TCfg Load <TCfg>(string filePath = "Configuration.json", bool?thorwException = null, bool?autoCreate = null, bool?autoSave = null, DateTimeOffset?overwriteObsolete = null)
        {
            if (thorwException == null)
            {
                thorwException = false;
            }
            if (autoCreate == null)
            {
                autoCreate = true;
            }
            if (autoSave == null)
            {
                autoSave = false;
            }

            try
            {
                if (string.IsNullOrWhiteSpace(filePath))
                {
                    throw new ArgumentOutOfRangeException(nameof(filePath));
                }

                if (overwriteObsolete != null)
                {
                    var lastWriteTime = FileWt.GetLastWriteTime(filePath);
                    if (overwriteObsolete > lastWriteTime)
                    {
                        FileWt.Delete(filePath);
                    }
                }


                if (autoCreate.Value && !FileWt.Exists(filePath))
                {
                    var cfg = (TCfg)typeof(TCfg).Create();
                    if (autoSave.Value)
                    {
                        Save(cfg, filePath, thorwException);
                    }
                    return(cfg);
                }

                var sConf = FileWt.ReadAllText(filePath);
                if (!string.IsNullOrWhiteSpace(sConf))
                {
                    return(sConf.FromJson <TCfg>());
                }
            }
            catch (Exception ex)
            {
                if (thorwException.Value)
                {
                    throw;
                }
            }

            return(default(TCfg));
        }
예제 #2
0
        public static void ImgSave(Image img, string directory, string preview, string fileName, ImageFormat imageFormat)
        {
            DirectoryWt.CreateDirectory(Path.Combine(directory, preview));

            var imgPath = Path.Combine(directory, preview, fileName);

            FileWt.Delete(imgPath);
            img.Save(imgPath, imageFormat);
            Report.WriteLine(imgPath, ConsoleColor.Blue);
        }