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>
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"));
}
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…