XmlDocument クラスを使用して値を直接変更することにより、インストール時に bindingRedirect 要素を変更しようとしています。私のapp.configは次のようになります。
<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 に変更しようとしました
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 もこのセクションにアクセスできません。