【问题标题】:Add Key to appsettings in web.config shows An error occurred loading a configuration file: Access to the path 'c:\inetpub\vobv5_zi.tmp' is deniedAdd Key to appsettings in web.config 显示加载配置文件时出错:访问路径 'c:\inetpub\vobv5_zi.tmp' 被拒绝
【发布时间】:2015-08-18 13:44:03
【问题描述】:

我正在尝试在 C://inetpub 文件夹中的 web.config 中添加 appsettings。 收到错误 加载配置文件时出错:拒绝访问路径“c:\inetpub\vobv5_zi.tmp”。 (c:\inetpub\web.config.config)

这是我的代码

在Vb中

Dim config As Configuration = ConfigurationManager.OpenExeConfiguration("c:\inetpub\" & sServer & "\web.config")
        config.AppSettings.Settings.Add(sSearch, "C:\Attachments\")
        config.Save(ConfigurationSaveMode.Modified)
        ConfigurationManager.RefreshSection("appSettings")

在 C# 中

   var config = ConfigurationManager.OpenExeConfiguration("c:\\inetpub\\web.config");
        config.AppSettings.Settings.Add("OS", "Linux");
        config.Save(ConfigurationSaveMode.Modified);

我在 VB 和 C# 中都试过了。 当我的 web.config 在项目中时,它正在添加 appsettings 而我的 web.config 在 inetpub 中,它正在抛出错误 加载配置文件时出错:访问路径“c:\inetpub\yzb2crt2.tmp”被拒绝。

【问题讨论】:

    标签: c# vb.net web-config appsettings


    【解决方案1】:

    运行代码的用户(不太清楚如何运行代码)需要对文件具有写权限。

    看起来很清楚,所以您可能需要向我们提供更多信息?

    【讨论】:

    • 这段代码是从哪里调用的?你给什么用户烫发了?为 web.config 文件提供权限可能还不够(因为它似乎首先写入 tmp 文件)。如果您在本地运行,请尝试添加“Everyone”以完全控制文件夹(并在测试后立即删除),如果可行,请尝试运行代码的用户(并删除)。
    • 当我以管理员模式运行我的 Visual Studio 2012 时,没有收到任何错误..但应用程序密钥未添加到应用程序设置中
    • 是的,我添加了所有人并以管理员模式启动我的 Visual Studio 没有收到任何错误,但 appsettings 没有添加
    • protected void Page_Load(object sender, EventArgs e) { var config = ConfigurationManager.OpenExeConfiguration("c:\\inetpub\\web.config"); config.AppSettings.Settings.Add("OS", "Linux"); config.Save(ConfigurationSaveMode.Full); ConfigurationManager.RefreshSection("appSettings"); }
    • 我正在测试我的页面加载@Mark ...你能看看这个
    【解决方案2】:

    终于找到了解决办法

      var xmlDoc = new XmlDocument();
            xmlDoc.Load("c:\\inetpub\\web2.config");
    
            var nodeRegion = xmlDoc.CreateElement("add");
            nodeRegion.SetAttribute("key", "Region");
            nodeRegion.SetAttribute("value", "Canterbury");
    
            xmlDoc.SelectSingleNode("//appSettings").AppendChild(nodeRegion);
            xmlDoc.Save("c:\\inetpub\\web2.config");
    
            ConfigurationManager.RefreshSection("appSettings");
    

    由此我在 inetpub 的 web.config 文件中添加 appsettings。

    【讨论】:

      猜你喜欢
      • 2012-10-31
      • 2012-05-02
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      相关资源
      最近更新 更多