我尝试在安装时通过使用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也没有访问此部分的权限。
发布于 2009-05-01 00:04:56
我找到了我需要的东西。XmlNamespaceManager是必需的,因为assemblyBinding节点包含xmlns属性。我修改了代码以使用它,它可以工作:
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"));
}发布于 2010-02-27 04:40:53
听起来你现在已经让你的配置文件调整工作了,但是我想你可能仍然对如何在运行时调整绑定重定向感兴趣。关键是使用AppDomain.AssemblyResolve事件,详细信息在this answer中。与使用配置文件相比,我更喜欢使用配置文件,因为我的版本号比较可能会更复杂一些,而且我不必在每次构建时都调整配置文件。
发布于 2009-04-30 22:30:08
我认为正确的Xpath语法是:
/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect@newVersion
(斜杠太多了)。
或者,如果这不起作用,您可以选择bindingRedirect元素(使用SelectSingleNode):
/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect
然后修改该元素的newVersion属性。
https://stackoverflow.com/questions/809262
复制相似问题