2008-10-28 45 views
9

Theo mặc định, tệp cấu hình của ứng dụng .NET được đặt tên sau "tên tệp exe" .config. Tôi tự hỏi liệu có thể có cấu hình của một ứng dụng được chỉ định động hay không.Có thể chuyển đổi tệp cấu hình ứng dụng khi chạy cho ứng dụng .NET không?

Ví dụ: ứng dụng được xây dựng là "foo.exe". Khi chạy, tệp cấu hình là "foo.exe.config". Có thể có nó chấp nhận các đối số dòng lệnh để sử dụng tập tin cấu hình khác. Vì vậy, ứng dụng có thể sử dụng cấu hình khác như dưới đây.

foo.exe /config:bar.config

bar.config được sử dụng như tập tin cấu hình insteand của foo.exe.config.

Trả lời

0

Có bạn sẽ cần phải sử dụng ExeConfigurationFileMap

+0

bạn có thể đưa ra một số thông tin chi tiết hoặc tham chiếu đến hướng dẫn? –

+0

Đây là ví dụ và tài liệu MSDN http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration.aspx –

4

Mã từ 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

nhận từ 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

Tất cả các công việc trên tốt nếu bạn cần phải thay thế chỉ phần AppSettings.

Trong trường hợp bạn phải chạy với các tệp cấu hình khác nhau (tất cả các phần), bạn có thể muốn xem xét khởi chạy ứng dụng bằng máy chủ, tạo miền ứng dụng cho ứng dụng chính của bạn và đặt tệp cấu hình khác nhau tùy thuộc vào thông số bạn đã nhập.

Dưới đây là đoạn code mà làm việc cho tôi:

 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"); 
Các vấn đề liên quan