zoukankan      html  css  js  c++  java
  • 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\。
  • 相关阅读:
    数与bit
    ARM汇编优化1
    一 *(a+1)与*(&a+1)
    二 *(a+1)多维数组
    三 二维数组取址
    四 sizeof(a)
    永恒之蓝及WannaCry分析
    github使用记录
    三种页面置换算法的C++模拟
    opencv检测图像直线
  • 原文地址:https://www.cnblogs.com/surfing/p/3056233.html
Copyright © 2011-2022 走看看