Ejemplo n.º 1
0
        public static bool Save <TCfg>(TCfg config, string cfgFilePath = "Configuration.json", bool?thorwException = null)
        {
            if (thorwException == null)
            {
                thorwException = false;
            }

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

                var sconf = config.ToJson(indented: true);
                FileWt.WriteAllText(cfgFilePath, sconf);

                return(true);
            }
            catch (Exception)
            {
                if (thorwException.Value)
                {
                    throw;
                }

                return(false);
            }
        }
Ejemplo n.º 2
0
 public static void Save(this MemoryStream ms, string path)
 {
     using (var fileStream = FileWt.Create(path))
     {
         ms.WriteTo(fileStream);
         fileStream.Close();
     }
 }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
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);
        }