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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/809262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:05:09  来源:igfitidea点击:

How to programmatically modify assemblyBinding in app.config?

c#xmlconfigurationconfiguration-filesxmldocument

提问by esac

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:

我试图在安装时通过使用 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>

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

然后我尝试使用以下代码将 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"));
}

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.

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

采纳答案by esac

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:

我找到了我需要的东西。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"));
    }

回答by Joe

I think the right Xpath syntax is:

我认为正确的 Xpath 语法是:

/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect@newVersion

/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect@newVersion

(you have a slash too many).

(你的斜线太多了)。

Or if this doesn't work you could select the bindingRedirect element (using SelectSingleNode):

或者,如果这不起作用,您可以选择 bindingRedirect 元素(使用 SelectSingleNode):

/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect

/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect

Then modify the attribute newVersion of this element.

然后修改这个元素的属性newVersion。

回答by Don Kirkby

Sounds like you've got your configuration file tweak working now, but I thought you might still be interested in how to adjust binding redirects at run time. The key is to use the AppDomain.AssemblyResolveevent, and the details are in this answer. I prefer it over using the configuration file, because my version number comparison can be a bit more sophisticated and I don't have to tweak the configuration file during every build.

听起来您现在已经调整了配置文件,但我认为您可能仍然对如何在运行时调整绑定重定向感兴趣。关键是使用AppDomain.AssemblyResolve事件,详细信息在这个答案中。我更喜欢它而不是使用配置文件,因为我的版本号比较可能更复杂一些,而且我不必在每次构建期间调整配置文件。