• 【转载】WebConfigurationManager和ConfigurationManager


    原文链接

    今天在写程序时偶然发现有的示例代码中是用WebConfigurationManager获取web.config中的配置信息的,因为之前一直都是用的ConfigurationManager,所以简单的深究了一下,并做个小小的总结。

    MSDN的说明:

    使用 WebConfigurationManager 可以访问计算机和应用程序信息。

    使用 WebConfigurationManager 是处理与 Web 应用程序相关的配置文件的首选方法。对于客户端应用程序,请使用 ConfigurationManager

    不知道这里是否可以理解为:

    1. 对于web应用程序而言,建议优先使用WebConfigurationManager ;但该方法不适用于客户端应用程序比如winform,WPF程序。

    2. ConfigurationManager ,即适用于web应用程序,也适用于客户端应用程序,但对于客户端应用程序来说更好。

      //对于客户端应用程序,请使用 ConfigurationManager。
            protected static string connectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
    
            protected static string appconfigString = ConfigurationManager.AppSettings[""].ToString();
    
            //使用 WebConfigurationManager 是处理与 Web 应用程序相关的配置文件的首选方法。
            protected static string webconnectionString = WebConfigurationManager.ConnectionStrings[""].ToString();
    
            protected static string webappconfigString = WebConfigurationManager.AppSettings[""].ToString();
    
    
            //Web.config中的配置节点:
    
            //ConnectionStrings
            //<connectionStrings>
            //  <add name="ConnStr" connectionString="Data Source=.;user ID=sa;Pwd=123456;Database=database;"/>
            //</connectionStrings>
    
            //AppSettings
            //<appSettings>
            //  <add key="" value=""/>
            //</appSettings>
    
    
            //写在 <appSettings >中用System.Configuration.ConfigurationManager.AppSettings["name"]检索值。 
            //写在 <ConnectionStrings>中用System.Configuration.ConfigurationManager.ConnectionStrings["name"]检索值。
    
    
            //注意:
            //System.Web.Configuration  默认没有在程序中添加引用,所以直接using无法using出来。
    复制代码
    

      

  • 相关阅读:
    期末考试(优先队列)
    看病要排队《优先队列》
    Windows Message Queue(优先队列)
    Stones(优先队列)
    懒省事的小明(优先队列)
    产生冠军(set,map,拓扑结构三种方法)
    Web轻量级扫描工具Skipfish
    Web侦察工具HTTrack (爬取整站)
    文件上传漏洞绕过技巧
    Python爬虫之selenium的使用(八)
  • 原文地址:https://www.cnblogs.com/flyant/p/4277013.html
Copyright © 2020-2023  润新知