2012-12-18 78 views
2

我有許多應用程序正在運行,它們之間相互通信,但這些應用程序都沒有自己的用戶界面。我有一個系統控制檯應用程序,它充當系統的用戶界面(即所有彼此交談的應用程序集合)。C#:讀取和修改其他應用程序的app.config文件中的設置

我希望能夠使用系統控制檯來讀取和修改每個非gui應用程序的配置。
每個應用程序都有一個使用Visual Studio設置GUI創建的app.config文件。這些設置都在應用範圍,這將導致一個app.config文件,它看起來有點像這樣:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<configSections> 
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > 
     <section name="ExternalConfigReceiver.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </sectionGroup> 
</configSections> 
<applicationSettings> 
    <ExternalConfigReceiver.Properties.Settings> 
     <setting name="Conf1" serializeAs="String"> 
      <value>3</value> 
     </setting> 
     <setting name="Conf2" serializeAs="String"> 
      <value>4</value> 
     </setting> 
    </ExternalConfigReceiver.Properties.Settings> 
</applicationSettings> 

我曾嘗試使用下面的代碼讀取配置設置嘗試

System.Configuration.ExeConfigurationFileMap fileMap = new System.Configuration.ExeConfigurationFileMap(); 
fileMap.ExeConfigFilename = "PATH_TO_THE_FOLDER\\app.config"; 

System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(fileMap, System.Configuration.ConfigurationUserLevel.None); 

someVariable = config.AppSettings.Settings["Conf1"]; 
someVariable2 = config.AppSettings.Settings["Conf2"]; 

但仔細檢查config.AppSettings對象後,我發現它沒有設置。

我在做什麼錯?我使用錯誤的方法來讀取配置文件?這種方法最適合不同類型的配置文件嗎?

+0

我覺得你的意思是'設置[「Conf1」]。值',你應該能夠得到一些東西,或者你會得到一個錯誤? – V4Vendetta

+0

如果運行時發生這種情況,那麼就會有沒有app.config文件 - 配置文件將獲得AppName.exe.config或任何主要可執行文件的名稱 – drk

+0

雖然這是可能的,但我建議不要這樣做 - 它允許一個應用程序(控制檯應用程序)通過搞亂其配置來破壞另一個應用程序,並強制理解您的各個應用程序配置的方式至少分佈在兩個地方。相反,我建議嚮應用程序添加「修改設置」消息,並讓消息處理程序更新設置。 –

回答

4

其可能的配置文件使用配置文件爲XML,然後使用XPath更改值:

using (TransactionScope transactionScope = new TransactionScope()) 
{ 
    XmlDocument configFile = new XmlDocument(); 

    configFile.Load("PathToConfigFile"); 

    XPathNavigator fileNavigator = configFile.CreateNavigator(); 

    // User recursive function to get to the correct node and set the value 
    WriteValueToConfigFile(fileNavigator, pathToValue, newValue); 

    configFile.Save("PathToConfigFile"); 

    // Commit transaction 
    transactionScope.Complete(); 
} 

private void WriteValueToConfigFile(XPathNavigator fileNavigator, string remainingPath, string newValue) 
{ 
    string[] splittedXPath = remainingPath.Split(new[] { '/' }, 2); 
    if (splittedXPath.Length == 0 || String.IsNullOrEmpty(remainingPath)) 
    { 
     throw new Exception("Path incorrect."); 
    } 

    string xPathPart = splittedXPath[0]; 
    XPathNavigator nodeNavigator = fileNavigator.SelectSingleNode(xPathPart); 

    if (splittedXPath.Length > 1) 
    { 
     // Recursion 
     WriteValueToConfigFile(nodeNavigator, splittedXPath[1], newValue); 
    } 
    else 
    { 
     nodeNavigator.SetValue(newValue ?? String.Empty); 
    } 
} 

可能的路徑CONF1:

"configuration/applicationSettings/ExternalConfigReceiver.Properties.Settings/setting[name=\"Conf1\"]/value"

-1

據我知道你不能使用System.Configuration.Configuration訪問其他應用程序的

它們是XML文件,你可以使用XML namspace與他們進行互動

相關問題