我有一个 app.config 文件。
我想检索和编辑一个属性:
<appSettings>
<add key="ProcessorInstances" value="2" />
</appSettings>
我正在使用这个查询:
var list5 = from appNode in doc2.Descendants("appSettings").Elements()
where appNode.Attribute("key").Value == "ProcessorInstances"
select appNode;
var element5 = list5.FirstOrDefault();
string five = element5.Attribute("value").Value;
但现在我面临这样的因素:
<app>
<file value="../../../logs/Intel.Service.log" />
</app>
和:
<baseAddresses>
<add baseAddress="http://Machinename:portnumber/servicename/" />
</baseAddresses>
这些元素没有任何键属性。
如何编写 LINQ 查询来编辑它们?
最佳答案
将 app.Config 文件解析为 xml 并使用 Linq 并不是从 App.Config 获取设置的方法。而是使用 System.Configuration.ddl(在您的项目中添加对此文件的引用)。
例如
string baseAddress = ConfigurationManager.AppSettings["baseAddress"].ToString();
关于c# - 使用 LINQ 更新 app.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11397711/