Пример #1
0
        /// <summary>加载指定配置文件</summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public virtual Boolean Load(String filename)
        {
            if (filename.IsNullOrWhiteSpace())
            {
                return(false);
            }
            filename = filename.GetFullPath();
            if (!File.Exists(filename))
            {
                return(false);
            }

            _loading = true;
            try
            {
                var data   = File.ReadAllBytes(filename);
                var config = this as TConfig;

                Object obj = config;
                var    xml = new NewLife.Serialization.Xml();
                xml.Stream       = new MemoryStream(data);
                xml.UseAttribute = false;
                xml.UseComment   = true;

                if (_.Debug)
                {
                    xml.Log = XTrace.Log;
                }

                if (!xml.TryRead(GetType(), ref obj))
                {
                    return(false);
                }

                config.ConfigFile = filename;
                config.SetExpire();  // 设定过期时间
                config.OnLoaded();

                return(true);
            }
            catch (Exception ex)
            {
                XTrace.WriteException(ex);
                return(false);
            }
            finally
            {
                _loading = false;
            }
        }
Пример #2
0
        /// <summary>加载指定配置文件</summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public virtual Boolean Load(String filename)
        {
            if (filename.IsNullOrWhiteSpace())
            {
                return(false);
            }
            filename = filename.GetFullPath();
            if (!File.Exists(filename))
            {
                return(false);
            }

            _loading = true;
            try
            {
                var config = this as TConfig;
                using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
                {
                    Object obj = config;
                    var    xml = new NewLife.Serialization.Xml();
                    xml.Stream       = stream;
                    xml.UseAttribute = false;
                    xml.UseComment   = true;
                    if (!xml.TryRead(GetType(), ref obj))
                    {
                        return(false);
                    }
                }

                config.ConfigFile = filename;
                config.SetExpire();  // 设定过期时间
                config.OnLoaded();

                return(true);
            }
            catch (Exception ex)
            {
                XTrace.WriteException(ex);
                return(false);
            }
            finally
            {
                _loading = false;
            }
        }