【问题标题】:Add a key value pair in config file at runtime in Azure Webrole在 Azure Webrole 运行时在配置文件中添加键值对
【发布时间】:2013-01-28 11:59:12
【问题描述】:

我想在运行时使用 c# 将键值对添加到 Azure Web 角色 web.config 文件。

非常感谢任何帮助。

【问题讨论】:

    标签: c# azure asp.net-mvc-4 web-config azure-web-roles


    【解决方案1】:

    这就是我们最终的做法。

    我们在 web.config 中添加了一个具有空白值的 Key,然后在运行时为其添加了一个值。我认为可以修改相同的代码以在运行时添加键和值对。

                using (var server = new ServerManager())
                {
                        var siteNameFromServiceModel = "Web";
                        var siteName =
                            string.Format("{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
    
                        string configFilePath = server.Sites[siteName].Applications[0].VirtualDirectories[0].PhysicalPath + "\\Web.config";
                        XElement element = XElement.Load(configFilePath);
    
                        element.Element("appSettings").Elements("add").Where(X => X.Attribute("key").Value == "YourKeyName").Single().Attribute("value").Value = "YourValue";
                        element.Save(configFilePath);
                 }
    

    【讨论】:

      猜你喜欢
      • 2012-02-21
      • 2020-01-13
      • 2018-03-19
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多