如何以编程方式修改app.config中的assemblyBinding?

编程入门 行业动态 更新时间:2024-10-25 17:20:10
本文介绍了如何以编程方式修改app.config中的assemblyBinding?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在安装时通过使用XmlDocument类并直接修改值来更改bindingRedirect元素。这是我的app.config样子:

I am trying to change the bindingRedirect element at install time by using the XmlDocument class and modifying the value directly. Here is what my app.config looks like:

<configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> ... </sectionGroup> </configSections> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="MyDll" publicKeyToken="31bfe856bd364e35"/> <bindingRedirect oldVersion="0.7" newVersion="1.0"/> </dependentAssembly> </assemblyBinding> </runtime> ... </configuration>

然后我尝试使用以下代码将1.0更改为2.0

I then try to use the following code to change 1.0 to 2.0

private void SetRuntimeBinding(string path, string value) { XmlDocument xml = new XmlDocument(); xml.Load(Path.Combine(path, "MyApp.exe.config")); XmlNode root = xml.DocumentElement; if (root == null) { return; } XmlNode node = root.SelectSingleNode("/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect/@newVersion"); if (node == null) { throw (new Exception("not found")); } node.Value = value; xml.Save(Path.Combine(path, "MyApp.exe.config")); }

但是,它会引发未找到异常。如果我将路径备份到/ configuration / runtime,那么它将起作用。但是,一旦我添加了assemblyBinding,它就找不到该节点。可能这与xmlns有关吗?知道我该如何修改吗? ConfigurationManager也无权访问此部分。

However, it throws the 'not found' exception. If I back the path up to /configuration/runtime it works. However once I add assemblyBinding, it does not find the node. Possibly this has something to do with the xmlns? Any idea how I can modify this? ConfigurationManager also does not have access to this section.

推荐答案

我找到了所需的内容。 XmlNamespaceManager是必需的,因为assemblyBinding节点包含xmlns属性。我修改了代码以使用此代码,它可以正常工作:

I found what I needed. The XmlNamespaceManager is required as the assemblyBinding node contains the xmlns attribute. I modified the code to use this and it works:

private void SetRuntimeBinding(string path, string value) { XmlDocument doc = new XmlDocument(); try { doc.Load(Path.Combine(path, "MyApp.exe.config")); } catch (FileNotFoundException) { return; } XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable); manager.AddNamespace("bindings", "urn:schemas-microsoft-com:asm.v1"); XmlNode root = doc.DocumentElement; XmlNode node = root.SelectSingleNode("//bindings:bindingRedirect", manager); if (node == null) { throw (new Exception("Invalid Configuration File")); } node = node.SelectSingleNode("@newVersion"); if (node == null) { throw (new Exception("Invalid Configuration File")); } node.Value = value; doc.Save(Path.Combine(path, "MyApp.exe.config")); }

更多推荐

如何以编程方式修改app.config中的assemblyBinding?

本文发布于:2023-11-04 01:13:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556550.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   app   assemblyBinding   config

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!