2016-11-22 100 views
0

我需要編輯不是運行代碼的可執行文件的配置文件(exefilename.config)。 XML文件的格式是這樣的:編輯另一個可執行文件的.config文件

<configSections> 
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <section name="XNet.XNetService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> 
     <section name="XNetService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> 
    </sectionGroup> 
</configSections> 
<applicationSettings> 
    <XNet.XNetService.Properties.Settings> 
     <setting name="DataSource" serializeAs="String"> 
      <value>192.168.2.127</value> 
     </setting> 
     <setting name="DefaultPort" serializeAs="String"> 
      <value>8085</value> 
     </setting> 
     <setting name="UserName" serializeAs="String"> 
      <value>notiuser</value> 
     </setting> 
     <setting name="Password" serializeAs="String"> 
      <value>6ITLet6k1HCOhgTmXI5C/1m1DrCQli8dIAKfdtP+o6w=</value> 
     </setting> 
     <setting name="Instance" serializeAs="String"> 
      <value /> 
     </setting> 
     <setting name="bindTo" serializeAs="String"> 
      <value /> 
     </setting> 
    </XNet.XNetService.Properties.Settings> 
</applicationSettings> 

我需要改變「DefaultPort」參數,「數據源」等,並在同一級別添加新的。我能怎麼做?如果.config文件是我的可執行文件,那麼使用簡單的SettingsWriter將會更容易,但在這種情況下,我該怎麼辦?

+0

看一看這太問題:http://stackoverflow.com/questions/4738/using-配置管理員加載配置從任意位置 – Klinger

+0

我假設你有這個問題,因爲你手動加載一個不同的應用程序的配置。爲什麼不能將配置複製到_your_應用的配置中,並使用適當的設置?或者使用用戶級別的設置使回寫更容易? –

+0

Klinger我在下面回答 – user3518663

回答

1

Web配置只是一個XML,所以爲什麼不把它當作一個呢?沿着這些線路的東西應該做的伎倆:

void ModifyConfig(string filepath, string xpath, string newValue) 
    { 
     System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); 
     doc.Load(filepath); 
     System.Xml.XmlNode elementToReplace = doc.DocumentElement.SelectSingleNode(xpath); 
     elementToReplace.InnerText = newValue; 
     doc.Save(filepath); 
    } 

,並調用它像:

 ModifyConfig("C:\\temp\\App.config", 
      "applicationSettings//XNet.XNetService.Properties.Settings//setting[@name='DefaultPort']//value", 
      "5000"); 

如果文件正在使用中,雖然你可能會出現問題。

0

我實現了Alex Nolasco建議的此線程中描述的代碼:Change default app.config at runtime

但是當我嘗試讀取屬性「數據源」像下面descripted,我仍然得到原始的.config值:-(

// the default app.config is used. 
using (AppConfig.Change(exePath)) 
{ 
    // the app.config in exePath is used 

    string test = Properties.Settings.Default.DataSource; 

} 
// the default app.config is used. 

我有一個疑問:我是臨時配置文件要讀得到了一個不同的assemby可這是問題

這是AppConfig的代碼:?

using System; 
using System.Configuration; 
using System.Linq; 
using System.Reflection; 

namespace XNet.XManager 
{ 
    public abstract class AppConfig : IDisposable 
    { 
     public static AppConfig Change(string path) 
     { 
      return new ChangeAppConfig(path); 
     } 

     public abstract void Dispose(); 

     private class ChangeAppConfig : AppConfig 
     { 
      private readonly string oldConfig = AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString(); 

      private bool disposedValue; 

      public ChangeAppConfig(string path) 
      { 
       AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path); 
       ResetConfigMechanism(); 
      } 

      public override void Dispose() 
      { 
       if (!disposedValue) 
       { 
        AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", oldConfig); 
        ResetConfigMechanism(); 


        disposedValue = true; 
       } 
       GC.SuppressFinalize(this); 
      } 

      private static void ResetConfigMechanism() 
      { 
       typeof(ConfigurationManager) 
        .GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static) 
        .SetValue(null, 0); 

       typeof(ConfigurationManager) 
        .GetField("s_configSystem", BindingFlags.NonPublic |BindingFlags.Static) 
        .SetValue(null, null); 

       typeof(ConfigurationManager) 
        .Assembly.GetTypes() 
        .Where(x => x.FullName == 
           "System.Configuration.ClientConfigPaths") 
        .First() 
        .GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static) 
        .SetValue(null, null); 
      } 
     } 
    } 
}