读取自己真实基础配置对象内容

[源码下载]

 获取真实基础配置对象

BaseConfigProvider.cs

/// <summary>
        
/// 获取真实基础配置对象
        
/// </summary>
        
/// <returns></returns>

         public   static  BaseConfigInfo GetRealBaseConfig()
        
{
            BaseConfigInfo newBaseConfig 
= null;
            
string filename = null;
            HttpContext context 
= HttpContext.Current;
            
if (context != null)
                filename 
= context.Server.MapPath("/BwConf.config");
            
else
                filename 
= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BwConf.config");

            
try
            
{
                newBaseConfig 
= (BaseConfigInfo)SerializationHelper.Load(typeof(BaseConfigInfo), filename);
            }

            
catch
            
{
                newBaseConfig 
= null;
            }


            
if (newBaseConfig == null)
            
{
                
try
                
{
                    BaseConfigInfoCollection bcc 
= (BaseConfigInfoCollection)SerializationHelper.Load(typeof(BaseConfigInfoCollection), filename);
                    
foreach (BaseConfigInfo bc in bcc)
                    
{
                        
if (Utils.GetTrueForumPath() == bc.Forumpath)
                        
{
                            newBaseConfig 
= bc;
                            
break;
                        }

                    }

                    
if (newBaseConfig == null)
                    
{
                        BaseConfigInfo rootConfig 
= null;
                        
foreach (BaseConfigInfo bc in bcc)
                        
{
                            
if (Utils.GetTrueForumPath().StartsWith(bc.Forumpath) && bc.Forumpath != "/")
                            
{
                                newBaseConfig 
= bc;
                                
break;
                            }

                            
if (("/").Equals(bc.Forumpath))
                            
{
                                rootConfig 
= bc;
                            }

                        }

                        
if (newBaseConfig == null)
                        
{
                            newBaseConfig 
= rootConfig;
                        }

                    }


                }

                
catch
                
{
                    newBaseConfig 
= null;
                }

            }


            
if (newBaseConfig == null)
            
{
                filename 
= context.Server.MapPath("~"+ "/BwConf.config";

                
try
                
{
                    newBaseConfig 
= (BaseConfigInfo)SerializationHelper.Load(typeof(BaseConfigInfo), filename);
                }

                
catch
                
{
                    newBaseConfig 
= null;
                }

            }


            
if (newBaseConfig == null)
            
{
                
throw new Exception("发生错误: 网站根目录下没有正确的BwConf.config文件");
            }

            
return newBaseConfig;

        }

 

SerializationHelper

/// <summary>
        
/// 反序列化
        
/// </summary>
        
/// <param name="type">对象类型</param>
        
/// <param name="filename">文件路径</param>
        
/// <returns></returns>

         public   static   object  Load(Type type,  string  filename)
        
{
            FileStream fs 
= null;
            
try
            
{
                
// open the stream...
                fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                XmlSerializer serializer 
= new XmlSerializer(type);
                
return serializer.Deserialize(fs);
            }

            
catch(Exception ex)
            
{
                
throw ex;
            }

            
finally
            
{
                
if(fs != null)
                    fs.Close();
            }

        }



        
/// <summary>
        
/// 序列化
        
/// </summary>
        
/// <param name="obj">对象</param>
        
/// <param name="filename">文件路径</param>

         public   static   void  Save( object  obj,  string  filename)
        
{
            FileStream fs 
= null;
            
// serialize it...
            try
            
{
                fs 
= new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                XmlSerializer serializer 
= new XmlSerializer(obj.GetType());
                serializer.Serialize(fs, obj);    
            }

            
catch(Exception ex)
            
{
                
throw ex;
            }

            
finally
            
{
                
if(fs != null)
                    fs.Close();
            }


        }

 

基本设置描述类, 加[Serializable]标记为可序列化

[Serializable]
    
public   class  BaseConfigInfo : IConfigInfo
    
{
        
私有字段

        
属性
    }

 

BwConf.config

<? xml version="1.0" ?>

< BaseConfigInfo  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
  
< Dbconnectstring > Data Source=(local);User ID=sqlname;Password=sqlpassword;Initial Catalog=databasename;Pooling=true </ Dbconnectstring >
  
< IsEncrypt > no </ IsEncrypt >
  
< GridviewDbconnectstring > Data Source=(local);Initial Catalog=BW_ERP_Data;User ID=sa;Password= </ GridviewDbconnectstring >
  
< Forumpath > / </ Forumpath >
  
< Founderuid > 0 </ Founderuid >
  
< Dbtype > SqlServer </ Dbtype >
</ BaseConfigInfo >

 

改自Discuz!NT

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值