Frage

Ich versuche, das bindingRedirect Element zu ändern, zum Zeitpunkt der Installation durch die XmlDocument-Klasse und den Wert direkt zu modifizieren. Hier ist, was meine app.config wie folgt aussieht:

<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>

ich dann versuchen, den folgenden Code zu verwenden, 1,0 bis 2,0

ändern
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"));
}

Es ist jedoch wirft die ‚nicht gefunden‘ Ausnahme. Wenn ich den Weg bis zu / configuration / Laufzeit zurück funktioniert es. Doch sobald ich assembly hinzufügen, wird es nicht um den Knoten zu finden. Möglicherweise hat dies etwas mit der xmlns zu tun? Jede Idee, wie ich das ändern kann? Konfigurationsmanager haben auch keinen Zugang zu diesem Abschnitt.

War es hilfreich?

Lösung

Ich fand, was ich brauchte. Die XmlNamespaceManager ist erforderlich, da die assembly Knoten enthält die Xmlns zuzuschreiben. Ich änderte den Code dieses zu verwenden und es funktioniert:

    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"));
    }
scroll top