2009-04-30 64 views
8

我想在安裝時通過使用XmlDocument類並直接修改值來更改bindingRedirect元素。這裏是我的app.config是什麼樣子:如何以編程方式修改app.config中的assemblyBinding?

 

<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也無法訪問此部分。

回答

8

我找到了我需要的東西。由於assemblyBinding節點包含xmlns屬性,因此需要XmlNamespaceManager。我修改了代碼使用這個和它的作品:

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")); 
    } 
+1

只需要注意,我會拋出異常,因爲這是安裝項目的一部分,這就是安裝程序得到錯誤通知的方式。如果修改完成,則方法返回true或false會更好。 – esac 2009-05-01 00:11:06

-1

我認爲正確的XPath語法是:

/配置/運行/ assemblyBinding/dependentAssembly/bindingRedirect @ NEWVERSION

(你有一個斜線太多)。

或者,如果這不起作用,你可以選擇bindingRedirect元(使用的SelectSingleNode):

/配置/運行/ assemblyBinding/dependentAssembly/bindingRedirect

然後修改這個元素的屬性NEWVERSION。

+0

已經走了這條路,它抱怨bindRedirect @ newVersion的無效標記。第二種情況,它抱怨說找不到指定的路徑。 – esac 2009-04-30 22:31:45

8

聽起來像是你有你的配置文件調整現在的工作,但我想你可能仍然感興趣的是如何在運行時調整綁定重定向。關鍵是要使用AppDomain.AssemblyResolve事件,詳情請見this answer。我更喜歡使用配置文件,因爲我的版本號比較可以更復雜一些,並且我不必在每次構建時調整配置文件。

+0

夥計!這應該是被接受的答案。 – Jupaol 2013-01-17 18:05:01

相關問題