asp.net中使用自定义的config文件

1、创建自定义文件夹和Appsetttings.config文件。

结构如下:

在appSettings中:

<?xml version="1.0"?>
<appSettings>
<add key="Test" value="Config/Test.config"/>

</appSettings>

在Test.config中:

<?xml version="1.0"?>
<appSettings>

<add key="UserInfo" value="001120" UserName="zhangsan" Sex="男"/>
</appSettings>

  

2、在web.config文件中添加Appsettings节点,并为其指定正确的路径(configSource)。

<appSettings configSource="Config\AppSetting.config"></appSettings>

3、一个使用自定义配置文件的Demo:

        public string Test()
        {
            string value = string.Empty;
            string UserName = string.Empty;
            string Sex = string.Empty;
            XmlDocument xml = new XmlDocument();
            xml.Load(Server.MapPath("../"+System.Configuration.ConfigurationManager.AppSettings["Test"].ToString()));
            //XmlNodeList list = xml.SelectNodes("/appSettings/add");
            //for (int n = 0; n < list.Count; n++)
            //{
            //    XmlElement item = (XmlElement)list.Item(n);
            //    value = item.GetAttribute("Value").ToString();
            //    UserName = item.GetAttribute("UserName").ToString();
            //    Sex = item.GetAttribute("Sex").ToString();
            //}
            XmlElement node = (XmlElement)xml.SelectSingleNode("/appSettings/add[@key='UserInfo']");
            value = node.GetAttribute("value").ToString();
            UserName = node.GetAttribute("UserName").ToString();
            Sex = node.GetAttribute("Sex").ToString();
            return value + UserName + Sex;
        }

最后附上Server.MapPath()的一些用法:

总注:Server.MapPath获得的路径都是服务器上的物理路径,也就是常说的绝对路径
1、Server.MapPath("/")
注:获得应用程序根目录所在的位置,如 C:\Inetpub\wwwroot\。
2、Server.MapPath("./")
注:获得所在页面的当前目录,等价于Server.MapPath("")。
3、Server.MapPath("../")
注:获得所在页面的上级目录。
4、Server.MapPath("~/")
注:获得当前应用级程序的目录,如果是根目录,就是根目录,如果是虚拟目录,就是虚拟目录所在的位置,如C:\Inetpub\wwwroot\Example\。

 

转载于:https://www.cnblogs.com/surfing/archive/2013/05/03/3056233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值