.net 读取独立的配置文件

前端之家收集整理的这篇文章主要介绍了.net 读取独立的配置文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

做底层项目经常会需要保存配置信息.为了方便运维,还是把配置文件单独分离出来配置比较好,一个项目一个单独的独立配置文件,简单,易维护,易移植.
全部都放在一个Web.config中.拥挤不堪,找起来麻烦,看着都烦.写说明文档也麻烦的要死..还是独立配置比较清爽..

下面是读取单独配置文件的类

 public class XMLConfigRead { public string ConfigPath; public XMLConfigRead(string fileName) { //HttpContext.Current.Request.PhysicalApplicationPath 取得config文件路径  string dllPath = string.Empty; try { //dllPath = HttpContext.Current.Request.PhysicalApplicationPath; dllPath = HttpRuntime.AppDomainAppPath;//Web环境下 这个最稳定.应该用这个. } catch (Exception ex) { dllPath = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).AbsolutePath); //本地应用程序的情况下.应该用这个 } if (File.Exists(fileName)) { ConfigPath = fileName; } else if (File.Exists(Path.Combine(dllPath,fileName))) { ConfigPath = Path.Combine(dllPath,fileName); } else { throw new Exception("配置文件不存在,请检查"); } } public string ReadNodeValue (string nodename) { XmlDocument doc = new XmlDocument(); doc.Load(ConfigPath); XmlNode node = doc.SelectSingleNode(nodename); if (node.Value == null) { if (node.Attributes["value"] == null) { return ""; }else{ return node.Attributes["value"].Value; } }else{ return node.Value; } } }

把 WebSiteErrorLog.config 配置文件,直接放在网站根目录下,
读取方法
XMLConfigRead ConfigRead = new XMLConfigRead(“WebSiteErrorLog.config”);

当然也可以把配置文件都放到config下面 读取方法得改成
XMLConfigRead ConfigRead = new XMLConfigRead(“config/WebSiteErrorLog.config”);

<?xml version="1.0" encoding="utf-8" ?>
<ErrorLog>
    <DB_DataSource value="10.90.0.0*"/>
    <DB_InitialCatalog value="Log"/>
    <DB_IntegratedSecurity value="false"/>
    <DB_MaxPoolSize value="10000"/>
    <DB_ConnectTimeout value="1"/>
    <DB_UserID value="sa"/>
    <DB_Password value="999"/>
    <WebSiteId value="123456789"/> 
</ErrorLog>

//使用方法,读取WebSiteErrorLog.config 配置文件 中的配置
见下面代码

public class StoreErrorToDB
    {
        XMLConfigRead ConfigRead = new XMLConfigRead("WebSiteErrorLog.config");

        static sqlConnectionStringBuilder connStr ;
        static string WebSiteId;
        public StoreErrorToDB()
        {
            if (connStr == null)
            {
                connStr = new sqlConnectionStringBuilder();
                connStr.DataSource = ConfigRead.ReadNodeValue("ErrorLog/DB_DataSource");
                connStr.InitialCatalog = ConfigRead.ReadNodeValue("ErrorLog/DB_InitialCatalog");
                connStr.IntegratedSecurity = Convert.ToBoolean(ConfigRead.ReadNodeValue("ErrorLog/DB_IntegratedSecurity"));
                connStr.UserID = ConfigRead.ReadNodeValue("ErrorLog/DB_UserID");
                connStr.Password = ConfigRead.ReadNodeValue("ErrorLog/DB_Password");

                //设置最大连接池
                connStr.MaxPoolSize = Convert.ToInt32(ConfigRead.ReadNodeValue("ErrorLog/DB_MaxPoolSize"));
                //设置超时时间为1秒
                connStr.ConnectTimeout = Convert.ToInt32(ConfigRead.ReadNodeValue("ErrorLog/DB_ConnectTimeout"));

                WebSiteId = ConfigRead.ReadNodeValue("ErrorLog/WebSiteId");
            }

        }
        public void SaveError(Error error)
        {  
                    sqlConnection conn =  new sqlConnection(connStr.ConnectionString);
                    conn.Open();
                    string sql = @" INSERT INTO Error
                                    (Id,WebSiteId,RequestQueryString,RequestUserAgent,RequestUserHostAddress,RequestUrl,RequestUrlReferrer,RequestRequestType,RequestHttpMethod,RequestContentEncoding,InnerException_Message,InnerException_Source,InnerException_StackTrace,InnerException_TargetSite,Message,Source,StackTrace,TargetSite,RequestCookie,RequestHeaders,RequestForms)
                                     VALUES 
                                    (@Id,@WebSiteId,@RequestQueryString,@RequestUserAgent,@RequestUserHostAddress,@RequestUrl,@RequestUrlReferrer,@RequestRequestType,@RequestHttpMethod,@RequestContentEncoding,@InnerException_Message,@InnerException_Source,@InnerException_StackTrace,@InnerException_TargetSite,@Message,@Source,@StackTrace,@TargetSite,@RequestCookie,@RequestHeaders,@RequestForms)"; sqlCommand cmd = conn.CreateCommand(); cmd.CommandText = sql; cmd.Parameters.Add("@RequestQueryString",sqlDbType.VarChar,4000); cmd.Parameters.Add("@RequestUserAgent",100); cmd.Parameters.Add("@RequestUserHostAddress",500); cmd.Parameters.Add("@RequestUrl",500); cmd.Parameters.Add("@RequestUrlReferrer",500); cmd.Parameters.Add("@RequestRequestType",10); cmd.Parameters.Add("@RequestHttpMethod",10); cmd.Parameters.Add("@RequestContentEncoding",10); cmd.Parameters.Add("@InnerException_Message",500); cmd.Parameters.Add("@InnerException_Source",500); cmd.Parameters.Add("@InnerException_StackTrace",600); cmd.Parameters.Add("@InnerException_TargetSite",600); cmd.Parameters.Add("@Message",500); cmd.Parameters.Add("@Source",500); cmd.Parameters.Add("@StackTrace",600); cmd.Parameters.Add("@TargetSite",600); cmd.Parameters.Add("@WebSiteId",50); cmd.Parameters.Add("@Id",sqlDbType.UniqueIdentifier,60); cmd.Parameters.Add("@RequestCookie",4000); cmd.Parameters.Add("@RequestHeaders",4000); cmd.Parameters.Add("@RequestForms",4000); cmd.Parameters[0].Value = error.RequestQueryString; cmd.Parameters[1].Value = error.RequestUserAgent; cmd.Parameters[2].Value = error.RequestUserHostAddress; cmd.Parameters[3].Value = error.RequestUrl; cmd.Parameters[4].Value = error.RequestUrlReferrer; cmd.Parameters[5].Value = error.RequestRequestType; cmd.Parameters[6].Value = error.RequestHttpMethod; cmd.Parameters[7].Value = error.RequestContentEncoding; cmd.Parameters[8].Value = error.InnerException_Message; cmd.Parameters[9].Value = error.InnerException_Source; cmd.Parameters[10].Value = error.InnerException_StackTrace; cmd.Parameters[11].Value = error.InnerException_TargetSite; cmd.Parameters[12].Value = error.Message; cmd.Parameters[13].Value = error.Source; cmd.Parameters[14].Value = error.StackTrace; cmd.Parameters[15].Value = error.TargetSite; cmd.Parameters[16].Value = WebSiteId; cmd.Parameters[17].Value = error.Id; cmd.Parameters[18].Value = error.RequestCookie; cmd.Parameters[19].Value = error.RequestHeaders; cmd.Parameters[20].Value = error.RequestForms; cmd.ExecuteNonQuery(); conn.Close(); } }
原文链接:https://www.f2er.com/xml/296316.html

猜你在找的XML相关文章