zoukankan      html  css  js  c++  java
  • C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作

    应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config)。

    配置文件,对于程序本身来说,就是基础和依据,其本质是一个xml文件,对于配置文件的操作,从.NET 2.0 开始,就非常方便了,提供了 System [.Web] .Configuration 这个管理功能的NameSpace,要使用它,需要添加对 System.configuration.dll的引用。

    对于WINFORM程序,使用 System.Configuration.ConfigurationManager;

    对于ASP.NET 程序, 使用 System.Web.Configuration.WebConfigurationManager;

    对于配置文件内容的读取,真是太普遍不过了,如果你的程序里,没有读取配置文件内容的方面,你都不好意思拿出来用

    我们以最常见的 AppSettings 小节来作为例子:

    假设有如下的配置文件内容:

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <appSettings>

        <add key="y" value="this is Y"/>

      </appSettings>

    </configuration>

    1. 读取值:

    • Asp.Net:   System.Web.Configuration.WebConfigurationManager.AppSettings[“y”];
    • WinForm:  System.Configuration.ConfigurationManager.AppSettings[“y”];

    2. 添加一项

    • ASP.NET(需要有写权限):

    Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

    AppSettingsSection app = config.AppSettings;

    app.Settings.Add("x", "this is X");

    config.Save(ConfigurationSaveMode.Modified);

    • WinForm:

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    AppSettingsSection app = config.AppSettings;

    app.Settings.Add("x", "this is X");

    config.Save(ConfigurationSaveMode.Modified);

    3. 修改一项

    • Asp.Net

    Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

    AppSettingsSection app = config.AppSettings;

    //app.Settings.Add("x", "this is X");

    app.Settings["x"].Value = "this is not Y";

    config.Save(ConfigurationSaveMode.Modified);

    • WinForm

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    AppSettingsSection app = config.AppSettings;

    //app.Settings.Add("x", "this is X");

    app.Settings["x"].Value = "this is not Y";

    config.Save(ConfigurationSaveMode.Modified);

    4. 删除一项

    • Asp.Net

    Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

    AppSettingsSection app = config.AppSettings;

    app.Settings.Remove("x");

    config.Save(ConfigurationSaveMode.Modified);

    • WinForm

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    AppSettingsSection app = config.AppSettings;

    app.Settings.Remove("x");

    config.Save(ConfigurationSaveMode.Modified);

    说明:需要注意的是,代码所修改的并不是app.config,而是[Application_Name].exe.config这个文件。其中Application_Name就是你的可执行文件的文件名,而[Application_Name].exe.config才是真正起作用的配置文件。至于app.config,把它理解为是初始化配置文件比较合适。对于winfom在vs调试下app.config无变化是正常的,bin里面生成的程序,运行可看到效果。

  • 相关阅读:
    UIWindow与UIView
    UIView与CALayer 区别
    setter getter 方法
    KVC、KVO 理解
    c语言实现单链表
    浅谈C的应用与常见error
    POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
    Google Code Jam 2008 Round 1A C Numbers(矩阵快速幂+化简方程,好题)
    POJ 3686 The Windy's(思维+费用流好题)
    POJ 2686 Traveling by Stagecoach(状压二维SPFA)
  • 原文地址:https://www.cnblogs.com/codealone/p/3332607.html
Copyright © 2011-2022 走看看