Пример #1
0
        public static bool TryRead <T>(string filepath,
                                       out List <T> list,
                                       out string message)
        {
            bool b = false;

            list    = null;
            message = string.Empty;
            try
            {
                if (File.Exists(filepath))
                {
                    //string filecontents = File.ReadAllText(filepath);
                    //if (!string.IsNullOrWhiteSpace(filecontents))
                    //{
                    //    list = JsonConvert.DeserializeObject<List<T>>(filecontents);

                    //    b = true;
                    //}
                    list = GenericObjectManager.ReadGenericList <T>(filepath);
                    b    = true;
                }
            }
            catch (Exception ex)
            {
                message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }

            return(b);
        }
Пример #2
0
        public static bool TryRead <T>(out List <T> list,
                                       out string message,
                                       string filepath) where T : class, new()
        {
            bool b = false;

            message = string.Empty;
            list    = new List <T>();
            //filepath = !String.IsNullOrWhiteSpace(filepath) ? filepath : Filepath<T>();
            try
            {
                if (File.Exists(filepath))
                {
                    list = GenericObjectManager.ReadGenericList <T>(filepath);
                    //string filecontents = File.ReadAllText(filepath);
                    //if (!string.IsNullOrWhiteSpace(filecontents))
                    //{

                    //    list = JsonConvert.DeserializeObject<List<T>>(filecontents);
                    //}
                    b = list.Count > 0;
                }
            }
            catch (Exception ex)
            {
                message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }
            return(b);
        }
Пример #3
0
        public static bool TryWrite <T>(T model, out string message, string filepath) where T : class, new()
        {
            bool b = false;

            message = string.Empty;
            //filepath = !String.IsNullOrWhiteSpace(filepath) ? filepath : Filepath<T>();
            try
            {
                //JsonSerializerOptions options = new JsonSerializerOptions()
                //{
                //    WriteIndented = true,
                //    IgnoreReadOnlyProperties = true,
                //    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                //};

                //var json = JsonConvert.SerializeObject(model, Formatting.Indented);
                //File.WriteAllText(filepath, json);
                GenericObjectManager.WriteGenericItem <T>(model, filepath);
                b = true;
            }
            catch (Exception ex)
            {
                message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }
            return(b);
        }
Пример #4
0
        public static void Write <T>(T t, bool isClearText) where T : class, new()
        {
            string filepath = GenerateFilepath <T>(isClearText);

            if (isClearText)
            {
                GenericObjectManager.WriteGenericItem <T>(t, filepath);
            }
            else
            {
                GenericObjectManager.WriteGenericItemBinary <T>(t, filepath);
            }
        }
Пример #5
0
        public static T Read <T>() where T : class, new()
        {
            T             t           = default(T);
            bool          isClearText = true;
            string        filename    = String.Empty;
            string        s           = GetModelName <T>().ToLower();
            var           folderpath  = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            DirectoryInfo directory   = new DirectoryInfo(folderpath);

            if (directory.Exists)
            {
                bool b     = false;
                var  files = directory.GetFiles();
                for (int i = 0; !b && i < files.Count(); i++)
                {
                    var    file      = files[i];
                    string name      = file.Name.ToLower();
                    string extension = file.Extension.ToLower();
                    if (name.Contains(s))
                    {
                        filename    = name;
                        isClearText = extension.Equals(".xml", StringComparison.OrdinalIgnoreCase);
                        b           = true;
                    }
                }
                if (!b)
                {
                    t = new T();
                }
                else
                {
                    string filepath = Path.Combine(folderpath, filename);
                    if (isClearText)
                    {
                        t = GenericObjectManager.ReadGenericItem <T>(filepath);
                    }
                    else
                    {
                        t = GenericObjectManager.ReadGenericItemBinary <T>(filepath);
                    }
                }
            }
            return(t);
        }