⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readconfig.cs

📁 Discuz集成.net源代码
💻 CS
字号:
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Configuration;
using System.Web;

namespace Motion.DZ
{
    public class ReadConfig
    {
        private string DbConn;
        private string tablePre;
        private string DbType;
        private string pwdKey;
        private string domain;
        private string PassMode;

        public ReadConfig()
        {
            string bp = ConfigurationManager.AppSettings["BbsPath"];
            string dp = ConfigurationManager.AppSettings["DntPath"];
            string BbsPath;
            string DntPath;
            string AllTexts;
            string AllTexts_1;

            if (!String.IsNullOrEmpty(bp))
            {
                bp = bp.Replace("/", @"\");
                if (!bp.EndsWith(@"\")) bp += @"\";
            }

            if (!String.IsNullOrEmpty(dp))
            {
                dp = dp.Replace("/", @"\");
                if (!dp.EndsWith(@"\")) dp += @"\";
            }
            
            BbsPath = String.Format("{0}config\\general.config", bp);
            DntPath = String.Format("{0}DNT.config", dp);
            AllTexts = File.ReadAllText(this.LocalPath(BbsPath));
            AllTexts_1 = File.ReadAllText(this.LocalPath(DntPath));
            this.DbConn = this.ReadXML(AllTexts_1, "Dbconnectstring");
            this.tablePre = this.ReadXML(AllTexts_1, "Tableprefix");
            this.DbType = this.ReadXML(AllTexts_1, "Dbtype");
            this.domain = this.ReadXML(AllTexts, "CookieDomain");
            this.pwdKey = this.ReadXML(AllTexts, "Passwordkey");
            this.PassMode = this.ReadXML(AllTexts, "Passwordmode");
        }

        private string LocalPath(string path)
        {
            string appPath = HttpContext.Current.Request.ApplicationPath.Length == 1 ? String.Empty : HttpContext.Current.Request.ApplicationPath;
            return (path.StartsWith(@"\"))
                ? HttpContext.Current.Server.MapPath(String.Format("{0}{1}", appPath, path))
                : path;
        }

        private string ReadXML(string ConfigText, string NodeName)
        {

            //因为只是读取,所以我在这里用的是正则提取有效信息。当然,你也可以用XML操作类。

            Regex r = new Regex(String.Format("<{0}>(.*)?</{0}>", NodeName), RegexOptions.IgnoreCase);
            if (r.IsMatch(ConfigText))
            {
                return r.Match(ConfigText).Groups[1].Value;
            }
            return String.Empty;
        }
        
        /// <summary>
        /// 论坛数据库链接字串
        /// </summary>
        public string BbsDbConnection
        {
            get
            {
                return this.DbConn;
            }
        }

        /// <summary>
        /// 论坛加密模式
        /// </summary>
        public int PasswordMode
        {
            get
            {
                int pm;
                Int32.TryParse(this.PassMode, out pm);
                return pm;
            }
        }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public string DBType
        {
            get
            {
                return this.DbType;
            }
        }

        /// <summary>
        /// 表前缀
        /// </summary>
        public string Tableprefix
        {
            get
            {
                return this.tablePre;
            }
        }

        /// <summary>
        /// Des-Key
        /// </summary>
        public string PasswordKey
        {
            get
            {
                return this.pwdKey;
            }
        }

        /// <summary>
        /// 作用域
        /// </summary>
        public string doMain
        {
            get
            {
                return this.domain;
            }
        }

        /// <summary>
        /// 当前用户IP
        /// </summary>
        public string UserIP
        {
            get
            {
                return System.Web.HttpContext.Current.Request.UserHostAddress;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -