Ejemplo n.º 1
0
        public static bool TryRead <T>(string filepath,
                                       out T model,
                                       out string message) where T : class, new()
        {
            bool b = false;

            model   = null;
            message = string.Empty;
            try
            {
                if (File.Exists(filepath))
                {
                    //string filecontents = File.ReadAllText(filepath);
                    //if (!string.IsNullOrWhiteSpace(filecontents))
                    //{
                    //    model = JsonConvert.DeserializeObject<T>(filecontents);
                    //    b = true;
                    //}
                    model = GenericObjectManager.ReadGenericItem <T>(filepath);
                    b     = true;
                }
            }
            catch (Exception ex)
            {
                message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }

            return(b);
        }
Ejemplo n.º 2
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);
        }