C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。
应用程序配置文件,对于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被人修改了。最终也没有解决这个问题
|
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文件被占用着不让改,然后用下面的方法
|
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 对象创建配置文件。的更多相关文章
- 无法为请求的 Configuration 对象创建配置文件 错误原因
Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 无法为请求的 Configura ...
- asp.net 2.0中新增的web.config的默认namespace功能 (转)
看上去这个题目比较长,但实际上,我在看资料时发现,这就是说,在asp.net 2.0中,只需要在web.config里定义你要用的那些namespace,则在aspx页面中就不需要再象1.1那样,用 ...
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html 应用程序配置文件,对于asp.net是 web.config,对 ...
- app.config/web.config配置文件增删改
一.概述 应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和 ...
- 【系统Configmachine.config与自己的应用程序的App.config/Web.Config配置节点重复】解决方法
自己的应用程序的App.config或Web.Config文件中与系统的C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Configmachine.co ...
- 读取、添加、删除、修改配置文件 如(Web.config, App.config)
private Configuration config; public OperateConfig() : this(HttpContext.Current.Request.ApplicationP ...
- ASP.NET程序中动态修改web.config中的设置项目(后台CS代码)
using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Dra ...
- 【转载】App.config/Web.config 中特殊字符的处理
写一个网站,遇到一个问题,发布以后,提示错误,但是即使打开错误提示(在web.config中打开),还是只提示错误,没提示什么地方错误,这让我知道了:是webconfig本身的错误,经过排除,是链接字 ...
- C#读取App.config/Web.config
读取需要添加 System.Configuration 引用, 两种方式添加: 1:.NETFramework程序可以在引用右击添加引用,然后添加System.Configuration 2:引入Nu ...
随机推荐
- <a>标签文字强制不换行
强制不换行 a{ white-space:nowrap; } 再补充说明所有关于换行的CSS样式: white-space: normal|pre|nowrap|pre-wrap|pre-line|i ...
- C#打印标签
一个复杂的标签包括一个复杂的表格样式和二维码.条形码等内容.所以如果直接绘制的方式将会非常的麻烦,所以采用使用的方案是使用模板的方式:1.使用Excel创建出想要的模板的样式.2.对模板中的动态内容进 ...
- JS 通过字符串取得对应对象
//对Array的扩展,查找所有满足条件的元素 Array.prototype.findAll = function (match) { var tmp = []; for (var i = 0; i ...
- imu_tk标定算法原理
imu_tk代码地址 https://bitbucket.org/alberto_pretto/imu_tk II. S ENSOR E RROR M ODEL 对于理想的IMU,加速度计三元组的3个 ...
- [No0000DC]C# FileHelper 本地文件、文件夹操作类封装FileHelper
using System; using System.Diagnostics; using System.IO; using System.Text; using Shared; namespace ...
- 关于struts中的表单元素- Form bean not specified on mapping for action: "helloa.do"报错
今天测试struts时仿照书上写了一个小的表单提交代码 <html:form action="helloa.do" method="post"> & ...
- mongodb操作文件
mongodb操作文件,主要是通过GridFS类.存储文件主要存放在fs中,其中的fs是数据库默认的.并且GridFS是直接与数据库打交道,与collection集合无关. ============= ...
- MyEvent.SetEvent; // 同步信号置位
MyEvent.SetEvent; // 同步信号置位 TSimpleEvent.Create = TEvent.Create(nil, True, False, nil) ...
- java之反射的基本介绍
什么是反射 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的以及动态调用对象的方法的功能称为Java的反射 ...
- linux常用查看文件或日志命令
常见查看文件内容命令汇总如下: cat filename 查看日志,会打开整个文件,直接跑到最后面 tac filename 查看日志,会打开整 ...