2013-01-09 99 views
-1

我想從我的項目更新另一個應用程序的app.config文件都在C#中。我知道如何更新它在內存中的項目,但不知道如何訪問我的其他項目的app.config文件。我有以下的代碼,但會改變我的當前項目不是另一個的app.config文件....感謝您的建議或想法更新配置文件

XmlDocument xmlDoc = new XmlDocument(); 

    xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 

    foreach (XmlElement element in xmlDoc.DocumentElement) 
    { 
     if (element.Name.Equals("appSettings")) 
     { 
      foreach (XmlNode node in element.ChildNodes) 
      { 
       if (node.Attributes[0].Value.Equals("Setting1")) 
       { 
        node.Attributes[1].Value = "New Value"; 
       } 
      } 
     } 
    } 
+0

我還沒有玩過這些類,但我想知道是否http://stackoverflow.com/questions/3912727/openmappedexeconfiguration-vs - 配置配置可能有任何用處。雖然不確定。 –

回答

1

好吧,如果顯示爲在app.config作品碼當前項目的文件,它應該爲另一個項目的app.config文件工作,您只需要從適當的路徑加載文件。換句話說,而不是做:

xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 

你必須將它更改爲:

xmlDoc.Load("Path\\to\\config\\file\\of\\other\\application"); 

或者更好的是,創建,是以路徑app.config文件作爲參數的新方法你可以根據你想要更改哪個文件來調用它....