gpt4 book ai didi

c# - 在 Azure Webrole 中运行时在配置文件中添加键值对

转载 作者:行者123 更新时间:2023-12-03 03:19:23 26 4
gpt4 key购买 nike

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

非常感谢任何帮助。

最佳答案

这就是我们最终的做法。

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

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

关于c# - 在 Azure Webrole 中运行时在配置文件中添加键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14561548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com