应用程序配置文件,对于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里面生成的程序,运行可看到效果。

事实上,运行时报错:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------

无法为请求的 Configuration 对象创建配置文件。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Configuration.ConfigurationErrorsException: 无法为请求的 Configuration 对象创建配置文件。

源错误:

行 13:         configuration.Save();//保存配置文件

后面查找帮助,说是:若要启用对远程服务器上配置设置的访问,请使用 Aspnet_regiis 命令行工具。
而Aspnet_regiis帮助中说的是:-config+ 允许对计算机上的 ASP.NET 配置进行远程访问。
但执行该命令后运行该项目仍然报同样错误。
再看帮助,说是:请注意,进行写入操作的用户或进程必须具有以下权限:
在当前配置层次结构级别下对配置文件和目录的写入权限。
对所有配置文件的读取权限。
但是我的文件访问权限是everyone完全控制,应当不会没有写入权限吧。
后面进入组策略编辑器,发现管理员用户居然没有启用,而现在使用的当前用户确实是Administrator,将用户名修改后,发现在组策略编辑器中不能启用管理员帐户,估计是这个版本的XP被人修改了。最终也没有解决这个问题

C# code?
1
2
3
4
5
            Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("/Web.config");
            AppSettingsSection app = webConfig.AppSettings;
            app.Settings[key].Value = value;
            webConfig.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");

这样运行到webConfig.Save(ConfigurationSaveMode.Modified)的时候出现【无法为请求的 Configuration 对象创建配置文件。】

开始的时候我以为config文件被占用着不让改,然后用下面的方法

C# code?
1
2
3
4
5
6
7
8
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("web.config"));
            XmlNode node;
            XmlElement element;
            node = doc.SelectSingleNode("//appSettings");
            element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
            element.SetAttribute("value", value);
            doc.Save(Server.MapPath("web.config"));

 

C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。的更多相关文章

  1. 无法为请求的 Configuration 对象创建配置文件 错误原因

    Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 无法为请求的 Configura ...

  2. asp.net 2.0中新增的web.config的默认namespace功能 (转)

    看上去这个题目比较长,但实际上,我在看资料时发现,这就是说,在asp.net 2.0中,只需要在web.config里定义你要用的那些namespace,则在aspx页面中就不需要再象1.1那样,用 ...

  3. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作

    原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html 应用程序配置文件,对于asp.net是 web.config,对 ...

  4. app.config/web.config配置文件增删改

    一.概述 应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和 ...

  5. 【系统Configmachine.config与自己的应用程序的App.config/Web.Config配置节点重复】解决方法

    自己的应用程序的App.config或Web.Config文件中与系统的C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Configmachine.co ...

  6. 读取、添加、删除、修改配置文件 如(Web.config, App.config)

    private Configuration config; public OperateConfig() : this(HttpContext.Current.Request.ApplicationP ...

  7. ASP.NET程序中动态修改web.config中的设置项目(后台CS代码)

    using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Dra ...

  8. 【转载】App.config/Web.config 中特殊字符的处理

    写一个网站,遇到一个问题,发布以后,提示错误,但是即使打开错误提示(在web.config中打开),还是只提示错误,没提示什么地方错误,这让我知道了:是webconfig本身的错误,经过排除,是链接字 ...

  9. C#读取App.config/Web.config

    读取需要添加 System.Configuration 引用, 两种方式添加: 1:.NETFramework程序可以在引用右击添加引用,然后添加System.Configuration 2:引入Nu ...

随机推荐

  1. win10 开启蓝 由于其配置信息(注册表中的)不完整或已损坏

    在管理员命令提示符下键入以下命令: Dism /Online /Cleanup-Image /ScanHealth 这条命令将扫描全部系统文件并和官方系统文件对比,扫描计算机中的不一致情况. Dism ...

  2. np.unravel_index

      >>> np.unravel_index([22, 41, 37], (7,6)) (array([3, 6, 6]), array([4, 5, 1]))>>> ...

  3. AppSettings操作类

    using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using ...

  4. PHP一个小函数

    // function makeTemp($fileName="index",$ftype=0) { $tempPath="xx/xxxx/{$fileName}.htm ...

  5. 阿里云mysql远程登录报ERROR 2027(HY000)

    mysql远程登录的命令是: mysql -h数据库地址 -u用户名 -p 但是用这个命令在登录阿里云的mysql时,会报ERROR 2027 (HY000): Malformed packet

  6. [No0000172]Android Studio设置HTTP代理(可用)

    android SDK下载:http://www.androiddevtools.cn . 禁止第一次启动 到AS安装目录,打开bin目录,编辑idea.properties, 在文件末尾添加: di ...

  7. 开启spark日志聚集功能

    spark监控应用方式: 1)在运行过程中可以通过web Ui:4040端口进行监控 2)任务运行完成想要监控spark,需要启动日志聚集功能 开启日志聚集功能方法: 编辑conf/spark-env ...

  8. vuex 的基本使用之Module

    Module 首先介绍下基本的组件化规则:你可以根据项目组件的划分来拆分 store,每个模块里管理着当前组件的状态以及行为,最后将这些模块在根 store 进行组合. const moduleA = ...

  9. 配置zsh

    .zshrc export ZSH="/root/.oh-my-zsh" ZSH_THEME="robbyrussell" plugins=(git zsh-s ...

  10. php之sphinx

    1.修改sphinx配置文件? source 数据源名称 { type = mysql ######数据源类型 sql_host = #######数据库主机地址(如果是外网,请确保防火墙允许链接) ...