加载类成员技巧

本文介绍了一种在.NET项目中配置并获取XML文件绝对路径的方法。通过在web.config中定义连接字符串来指定XML数据库的位置,并利用HttpServerUtility的MapPath方法转换为服务器上的实际路径。

 今天写了一个读取Xml数据库的实例,其中在web.config中配置了xml的绝对路径,我们要读取这些路径

<connectionStrings>
  <add name="BOARDFILEPATH" connectionString="~/XmlDatabase/Board.xml"/>
  <add name="TITLEFILEPATH" connectionString="~/XmlDatabase/Title.xml"/>
  <add name="REPLYFILEPATH" connectionString="~/XmlDatabase/Reply.xml"/>
  <add name="ATTACHMENTFILEPATH" connectionString="~/XmlDatabase/Attachment.xml"/>
  <add name="MESSAGEFILEPATH" connectionString="~/XmlDatabase/Message.xml"/>
  <add name="MESSAGESHIELDFILEPATH" connectionString="~/XmlDatabase/MessageShield.xml"/>
  <add name="USERSTATFILEPATH" connectionString="~/XmlDatabase/UserStat.xml"/>
 </connectionStrings>

这些路径都是在项目中的,所以我们读到它的绝对路径,需要用到server.MapPath,在这里,我们创建了一个用来封装这些路径的类

public class XmlBBS
 {

  public XmlBBS()
  {
   ///
  }

  private static string boardFilePath;
  public static string BoardFilePath
  {
   get { return boardFilePath; }
   set { boardFilePath = value; }
  }
  private static string titleFilePath;
  public static string TitleFilePath
  {
   get { return titleFilePath; }
   set { titleFilePath = value; }
  }
  private static string replyFilePath;
  public static string ReplyFilePath
  {
   get { return replyFilePath; }
   set { replyFilePath = value; }
  }
  private static string attachmentFilePath;
  public static string AttachmentFilePath
  {
   get { return attachmentFilePath; }
   set { attachmentFilePath = value; }
  }
  private static string messageFilePath;
  public static string MessageFilePath
  {
   get { return messageFilePath; }
   set { messageFilePath = value; }
  }
  private static string messageShieldFilePath;
  public static string MessageShieldFilePath
  {
   get { return messageShieldFilePath; }
   set { messageShieldFilePath = value; }
  }

  private static string userStatFilePath;
  public static string UserStatFilePath
  {
   get { return userStatFilePath; }
   set { userStatFilePath = value; }
  }

        public static void SystemInit(HttpServerUtility server)
        {
            boardFilePath = server.MapPath(
                ConfigurationManager.ConnectionStrings["BOARDFILEPATH"].ConnectionString
                );
            titleFilePath = server.MapPath(
                ConfigurationManager.ConnectionStrings["TITLEFILEPATH"].ConnectionString
                );
            replyFilePath = server.MapPath(
                ConfigurationManager.ConnectionStrings["REPLYFILEPATH"].ConnectionString
                );
            attachmentFilePath = server.MapPath(
                ConfigurationManager.ConnectionStrings["ATTACHMENTFILEPATH"].ConnectionString
                );
            messageFilePath = server.MapPath(
                ConfigurationManager.ConnectionStrings["MESSAGEFILEPATH"].ConnectionString
                );
            messageShieldFilePath = server.MapPath(
              

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值