zoukankan      html  css  js  c++  java
  • web.config和app.config的区别

    1.web.config和app.config的区别
    web.config是web应用程序的配置文件,为web应用程序提供相关的配置。而app.config是桌面应用程序会自动与你的程序进行关联。

    2.对这2个配置文件的说明
    (1)<appSettings>节点
    <appSettings>节点主要用来存储应用程序的配置信息。例如网站上传文件的类型
    例如:
    <appSettings>
    <!--允许上传的格式类型>
    <add key="ImageType" value=".jpg;.bmp,png">
    <!--允许上传的文件类型-->
    <add key="FileType" value=".jpg;.bmp;.gif">
    </appSettings>
    访问appSetting节点的value.
    string fileType=ConfigurationManager.AppSettings["FileType"];
    (2)<connectionStrings>
    <connectionStrings>节点主要用于配置数据库连接的。

    (3)自定义节点的配置解析
    方式一:Property
    <configuration>
    <configSections>
    <section name="Test1" type="Demo.Section1,Demo"/> ---为了使得配置节点被解析,需要在<configSections>中注册
    </configSections>
    <Test1 UserName="ahyok" Path="www.cnblogs.com/aehyok"></Test1>
    </configuration>
    实现代码为:
    namespace Demo
    {
    public class Section1 : ConfigurationSection
    {
    [ConfigurationProperty("UserName")]
    public string UserName
    {
    get { return this["UserName"].ToString(); }
    set { this["UserName"] = value; }
    }

    [ConfigurationProperty("Path")]
    public string Path
    {
    get { return this["Path"].ToString(); }
    set { this["Path"] = value; }
    }
    }
    }
    方式二:Elemnent
    <configuration>
    <configSectionsections>
    <section name="Test2" type="Demo.Section2.Demo"/>
    </configSections>
    <Test2>
    <Users UserName="aehyok" Password="123456"></Users>
    </Test2>
    </configuration>
    实现代码为:
    namespace Demo
    {
    public class Section2:configSection{
    [ConfigurationProperty("Users",isRequired=true)]
    public SectionElement Users
    {
    get{return (SectionElement)this["Users"];}
    }
    public class SectionElement:ConfigurationElement
    {
    [ConfigurationProperty["UserName"],isRequired=true]
    public string UserName
    {
    get{return this["UserName"].ToString();}
    set{this['UserName']=value;}
    }
    [ConfigurationProperty("Password", IsRequired = true)]
    public string Password
    {
    get { return this["Password"].ToString(); }
    set { this["Password"] = value; }
    }
    }
    }
    }
    方式三:--CDATA
    方式四:--Collection
    <configuration>
    <configSections>
    <section name="Test4" type="Demo.Section4,Demo"/>
    </configSections>
    <Test4>
    <add key="1" value="aehyok"></add>
    <add key="2" value="Leo"></add>
    <add key="3" value="Lynn"></add>
    </Test4>
    </configuration>
    注释:为每个集合中的参数项创建一个从ConfigurationElement继承的派生类,可参考Section1。
    为集合创建一个从ConfigurationElementCollection继承的集合类,具体在实现时主要就是调用基类的方法。
    在创建ConfigurationSection的继承类时,创建一个表示集合的属性就可以了,注意[ConfigurationProperty]的各参数。
    代码实现:
    namespace Demo
    {
    public class Section4 : ConfigurationSection
    {
    private static readonly ConfigurationProperty s_property
    = new ConfigurationProperty(string.Empty, typeof(MyKeyValueCollection), null,
    ConfigurationPropertyOptions.IsDefaultCollection);

    [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
    public MyKeyValueCollection KeyValues
    {
    get
    {
    return (MyKeyValueCollection)base[s_property];
    }
    }
    }

    [ConfigurationCollection(typeof(MyKeyValueSetting))]
    public class MyKeyValueCollection : ConfigurationElementCollection // 自定义一个集合
    {
    // 基本上,所有的方法都只要简单地调用基类的实现就可以了。

    public MyKeyValueCollection()
    : base(StringComparer.OrdinalIgnoreCase) // 忽略大小写
    {
    }

    // 其实关键就是这个索引器。但它也是调用基类的实现,只是做下类型转就行了。
    new public MyKeyValueSetting this[string name]
    {
    get
    {
    return (MyKeyValueSetting)base.BaseGet(name);
    }
    }

    // 下面二个方法中抽象类中必须要实现的。
    protected override ConfigurationElement CreateNewElement()
    {
    return new MyKeyValueSetting();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
    return ((MyKeyValueSetting)element).Key;
    }

    // 说明:如果不需要在代码中修改集合,可以不实现Add, Clear, Remove
    public void Add(MyKeyValueSetting setting)
    {
    this.BaseAdd(setting);
    }
    public void Clear()
    {
    base.BaseClear();
    }
    public void Remove(string name)
    {
    base.BaseRemove(name);
    }
    }

    public class MyKeyValueSetting : ConfigurationElement // 集合中的每个元素
    {
    [ConfigurationProperty("key", IsRequired = true)]
    public string Key
    {
    get { return this["key"].ToString(); }
    set { this["key"] = value; }
    }

    [ConfigurationProperty("value", IsRequired = true)]
    public string Value
    {
    get { return this["value"].ToString(); }
    set { this["value"] = value; }
    }
    }

    读取配置文件信息
    四个Read进行读取数据的代码如下:
    方式一:Property
    Section1 sectioin1 = (Section1)ConfigurationManager.GetSection("Test1");
    txtUserName.Text = sectioin1.UserName;
    txtPath.Text = sectioin1.Path;
    方式二:Element
    Section2 sectioin2 = (Section2)ConfigurationManager.GetSection("Test2");
    txtUName.Text = sectioin2.Users.UserName;
    txtPassword.Text = sectioin2.Users.Password;
    方式三:CDATA
    Section3 section3 = (Section3)ConfigurationManager.GetSection("Test3");
    T1.Text = section3.T1.CommandText.Trim();
    T2.Text = section3.T2.CommandText.Trim();
    方式四:Collection
    Section4 section4 = (Section4)ConfigurationManager.GetSection("Test4");
    txtKeyValues.Text = string.Join("",
    ( kv in section4.KeyValues.Cast<MyKeyValueSetting>()
    let s = string.Format("{0}={1}", kv.Key, kv.Value)
    s).ToArray());
    写入配置文件
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    方式一:Property
    Section1 sectioin1 = config.GetSection("Test1") as Section1;
    sectioin1.UserName = txtUserName.Text;
    sectioin1.Path = txtPath.Text;
    config.Save();
    其他方式类推。。。

  • 相关阅读:
    HDU 6214 Smallest Minimum Cut 最小割,权值编码
    HDU 6196 happy happy happy 爆搜加剪枝
    2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Coin 概率+矩阵快速幂
    HDU 6199 2017沈阳网络赛 DP
    HDU 6200 2017沈阳网络赛 树上区间更新,求和
    HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组
    docker平时使用异常记录
    AI模型运维——NVIDIA驱动、cuda、cudnn、nccl安装
    python——平时遇到问题记录
    python——虚拟环境管理大合集
  • 原文地址:https://www.cnblogs.com/minfan/p/6037690.html
Copyright © 2011-2022 走看看