c# 从 ASPX 页面编辑和更新 web.config 文件

发布于 2024-12-04 08:16:21 字数 718 浏览 1 评论 0原文

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

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

我已在 ASPX 页面的代码后面编写了这些键

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 文件的访问被拒绝,并且源错误框中没有显示相关源行。

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

谁能帮助我吗?

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" />

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

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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

折戟 2024-12-11 08:16:21

这篇 msdn 文章似乎认为您需要定义一个空字符串来打开默认配置项。

http://msdn.microsoft.com/en-us/library/ms151456.aspx

如果您这样做:

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

这应该允许您打开配置进行读/写,但正如其他人所说,这不是存储此类信息的最佳位置

This msdn article seems to think you will want to define a null string to open the default config item.

http://msdn.microsoft.com/en-us/library/ms151456.aspx

If you do:

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

This should allow you to open the config to read/write, but as others have said this is not the best place to store this type of info

吾性傲以野 2024-12-11 08:16:21

检查配置文件是否设置为只读?

PS - 如果您在网络配置中存储临时信息,您的设计可能有问题。

Check the config file is not set as read only?

PS - there is probably something wrong with your design if you are storing temporary information in the web config.

别靠近我心 2024-12-11 08:16:21

无法从 ASP.NET 应用程序编辑 web.config

理论上,.NET Framework 提供了 api 来编辑/保存应用程序配置文件(对于 ASP.NET 应用程序来说是 web.config)中的更改,但实际上在 IIS 中这不起作用,因为考虑到每个每次更改 web.config 时,IIS 应用程序都会重新启动,因此 .NET 不允许您保存以防止这种情况发生。

如果您确实需要允许从 ASPX 页面更改配置,您应该保存在数据库或其他外部 xml 文件中,然后在需要时读取这些设置。

web.config cannot be edited from an ASP.NET application.

in theory .NET Framework provides the apis to edit/save changes in the app configuration files (which for ASP.NET applications is the web.config) but in practice in IIS this does not work because consider every single time the web.config is changed the IIS Application is restarted so .NET does not allow you saving to prevent this.

if you really need to allow changes in configuration from the ASPX pages you should either save in the database or in other external xml files then read those settings when needed.

千と千尋 2024-12-11 08:16:21

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

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

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

Then you just call

editWebConfig("atlasuser", txtUsername.Text, Server.MapPath(".") + "\\web.config");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文