利用WebConfigurationManager修改web.config 问题

ly198706 2010-09-28 02:27:26

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetAppSetting("del_flag", "1");
}
}
private void SetAppSetting(string key, string val)
{
Configuration config = WebConfigurationManager.OpenMachineConfiguration(地址);

string strExists = "";
try
{
strExists = config.AppSettings.Settings[key].Value;
}
catch
{

}

if (strExists == "")
{
config.AppSettings.Settings.Add(key, val);
}
else
{
config.AppSettings.Settings[key].Value = val;
}
config.Save();
}

上面红色的地址,我用了Request.ApplicationPath
"~/"
"~"
"C:\\Documents and Settings\\Administrator\\桌面\\text"
这几种方式 都有错.说不能为\等东西开头或结尾.
没有提示这个错误的则直接找到的是.machine.config这个配置文件
而且还在这个文件中添加了


<location path="~" inheritInChildApplications="true">
<appSettings>
<add key="del_flag" value="1" />
</appSettings>
</location>


怎么才能获取到当前项目的位置文件.web.config.地址应该怎么写..
...全文
263 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-09-29
  • 打赏
  • 举报
回复
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
wangjunfeng2010 2010-09-29
  • 打赏
  • 举报
回复
还不如手动修改呢!
修改后会导致服务重新启动。
ly198706 2010-09-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 tianshikuqi8 的回复:]

WebConfigurationManager.OpenMachineConfiguration
你找的就是MachineConfig文件啊!
WebConfigurationManager.OpenWebConfiguration("~");
使用这个方法!~
[/Quote]
我也用了.来提问之前,我已经在网上找了N中写法.都有问题.
不是提示不能以/等东西开头或结尾.
就是直接找到machine.config这个配置文件.
辰爸 2010-09-28
  • 打赏
  • 举报
回复
WebConfigurationManager.OpenMachineConfiguration
你找的就是MachineConfig文件啊!
WebConfigurationManager.OpenWebConfiguration("~");
使用这个方法!~
myhope88 2010-09-28
  • 打赏
  • 举报
回复
这种方式实现不了,就换种方式去实现嘛,但个人还是建议不要修改web.config吧,你最好自己定义个config的,不然修改web.config之后,会导致程序重启的

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧