c#编辑并更新ASPX页面中的web.config文件 [英] c# edit and update a web.config file from ASPX page

查看:217
本文介绍了c#编辑并更新ASPX页面中的web.config文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的web.config中有两个键,我需要在ASPX页面中进行编辑,键是..

I have two keys in my web.config that I need to make available for editing in an ASPX page the keys are..

<add key="atlasuser" value="username" />
<add key="atlaspass" value="password" />

我已经在我的ASPX页面的代码中写了这个

I have written this in the code behind for my ASPX Page

Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
string u_user = txtUsername.Text;
string u_pass = txtPassword.Text;

string crmSID = Request.QueryString["SID"];

config.AppSettings.Settings["atlasuser"].Value = u_user;
config.AppSettings.Settings["atlaspass"].Value = u_pass;

config.Save(ConfigurationSaveMode.Full);

当我编辑字段并单击保存时,我收到一条解析错误消息,指出访问.tmp文件被拒绝并且在源错误框中没有显示相关的源代码行。

When I edit the fields and click save I get an parse error message stating that access to a .tmp file is denied and showing no relevant source lines in the source error box.

我正在运行Windows 7,我已经检查过NETWORK SERVICE对目录具有完全读写权限。

I am running windows 7 and I have checked that NETWORK SERVICE has full read write permissions to the directory.

任何人都可以帮助我吗?

Can anyone help me?

推荐答案

你可以编辑web.config文件本身。只需确保您运行应用程序的帐户具有相应的文件系统权限,即可写入应用程序目录:

You can edit the web.config file itself. Just make sure that the account you are running your application under has the appropriate file system permissions to be able to write to the application directory:

void editWebConfig(string key, string newValue, string filePath)
{
    var xml = new XmlDocument();
    xml.Load(filePath);
    using(FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
    {
        xml.SelectSingleNode("//configuration/appSettings/add[@key = '" + key + "']").Attributes["value"].Value = newValue;
        xml.Save(fs);
    }
}

然后你只需拨打

editWebConfig("atlasuser", txtUsername.Text, Server.MapPath(".") + "\\web.config");

这篇关于c#编辑并更新ASPX页面中的web.config文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆