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被人修改了。最终也没有解决这个问题

 

 

 

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" ));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值