2008-10-28 7 views
9

Standardmäßig ist die Konfigurationsdatei der .NET-Anwendung nach "exe file name" .config benannt. Ich frage mich, ob es möglich ist, die Konfiguration einer Anwendung dynamisch festzulegen.Ist es möglich, die Anwendungskonfigurationsdatei zur Laufzeit für die .NET-Anwendung zu wechseln?

Zum Beispiel ist die erstellte Anwendung "foo.exe". Zur Laufzeit ist die Konfigurationsdatei "foo.exe.config". Ist es möglich, dass es Befehlszeilenargumente akzeptiert, um andere Konfigurationsdateien zu verwenden? So kann die Anwendung andere Konfiguration wie unten verwenden.

foo.exe /config:bar.config

bar.config wird als Konfigurationsdatei insteand von foo.exe.config verwendet.

Antwort

0

Ja, Sie verwenden ExeConfigurationFileMap

+0

können Sie einige Details oder Verweis auf Tutorials geben? –

+0

Hier ist das MSDN-Beispiel und die Dokumentation http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration.aspx –

4

-Code von MSDN

static void DisplayMappedExeConfigurationFileSections() 
{ 
    // Get the application configuration file path. 
    string exeFilePath = System.IO.Path.Combine(
     Environment.CurrentDirectory, "ConfigurationManager.exe.config"); 
    // HERE !!!  
    // Map to the application configuration file. 
    ExeConfigurationFileMap configFile = new ExeConfigurationFileMap(); 
    configFile.ExeConfigFilename = exeFilePath; 

    Configuration config = 
     ConfigurationManager.OpenMappedExeConfiguration(configFile, 
     ConfigurationUserLevel.None); 

    // Display the configuration file sections. 
    ConfigurationSectionCollection sections = config.Sections; 

    Console.WriteLine(); 
    Console.WriteLine("Sections in machine.config:"); 

    // Loop to get the sections machine.config. 
    foreach (ConfigurationSection section in sections) 
    { 
     string name = section.SectionInformation.Name; 
     Console.WriteLine("Name: {0}", name); 
    } 

} 
3

Gotten von How to use Configuration.GetSection() and ConfigurationManager.OpenMappedExeConfiguration()

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); 
fileMap.ExeConfigFilename = @"C:\Inetpub\Test\Config\Dev.config"; 
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); 
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings"); 
string ConfigVersion = section.Settings["ConfigVersion"].Value; 
10

Alle der oben genannten Arbeit gut brauchen, wenn Sie nur AppSettings Abschnitt ersetzen müssen.

Falls Sie mit verschiedenen Konfigurationsdateien (alle Abschnitte) arbeiten müssen, sollten Sie eine Anwendung mit einem Host starten, die eine Anwendungsdomäne für Ihre Hauptanwendung erstellt und abhängig von den von Ihnen übergebenen Parametern eine andere Konfigurationsdatei erstellt.

Hier ist der Code, der für mich gearbeitet:

 AppDomainSetup setup = new AppDomainSetup(); 
     setup.ApplicationBase = "file://" + System.Environment.CurrentDirectory; 
     setup.DisallowBindingRedirects = true; 
     setup.DisallowCodeDownload = true; 

     if (args.Length != 0 && args[0].Equals("-test")) 
     { 
      setup.ConfigurationFile = "PATH_TO_YOUR_TEST_CONFIG_FILE"; 
     } 
     else { 
      setup.ConfigurationFile = "PATH_TO_YOUR_LIVE_CONFIG_FILE"; 
     } 

     AppDomain domain = AppDomain.CreateDomain("FRIENDLY_NAME", null, setup); 
     domain.ExecuteAssembly("YourMainApp.exe"); 
Verwandte Themen