简体   繁体   English

如何使用不同的配置文件运行可执行程序?

[英]How to run a executable program with different configuration file?

I want to run a program with different configuration file, the program write with C# 2.0, I make some different file name {program_name}.exe.config, I mean one exe with different config file, for example I have 3 config file, then I will run 3 exe with the different config file, bu the exe file is the same one. 我想用不同的配置文件运行一个程序,程序用C#2.0编写,我做了一些不同的文件名{program_name} .exe.config,我的意思是一个exe用不同的配置文件,例如我有3个配置文件,然后我将使用不同的配置文件运行3个exe,exe文件是同一个。 Can I do not modify the program for read the different config file (I don`t want to put the config file path in the exe command parameters) to do that(like use the batch file or other method.) ? 我是否可以不修改程序以读取不同的配置文件(我不想将配置文件路径放在exe命令参数中)来执行此操作(如使用批处理文件或其他方法。)?

Thanks. 谢谢。

You can change the configuration file for the application domain in which the exe is loaded. 您可以更改加载exe的应用程序域的配置文件。 This is done using the SetData method of the AppDomain class. 这是使用AppDomain类的SetData方法完成的。 Ensure that this line of code is executed as the first line of your application. 确保将此行代码作为应用程序的第一行执行。

I have used following code to share 1 exe.config file between 3 different executables. 我使用以下代码在3个不同的可执行文件之间共享1个exe.config文件。

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE","yourSharedConfig.exe.config");

You can look at the following blog entry 您可以查看以下博客条目
Binding to custom app.config 绑定到自定义app.config

If you want to run the same exe with 3 different configs, I believe the same approach will work with bit of customization. 如果你想用3种不同的配置运行相同的exe,我相信相同的方法将适用于一些自定义。 You can pass the name of the config file while invoking the exe as a command line parameter and using the SetData method you can dynamically set the config. 您可以在调用exe作为命令行参数时传递配置文件的名称,并使用SetData方法可以动态设置配置。

I make it with LINQ and passing the parameter as config=path2file 我使用LINQ创建它并将参数作为config = path2file传递

public partial class App : Application {

    private void startup(object sender, StartupEventArgs e) {
        if (null != e) {
            if (null != e.Args && 0 < e.Args.Length) {
                string config = e.Args.Where(a => a.StartsWith("config=")).FirstOrDefault();
                if (null != config) {
                    config = config.Substring("config=".Length);
                    if (File.Exists(config)) {
                        AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", config);
                    }
                }
            }
        }
    }

The main issue you have with three configs and one executable is that you need to specify to the executable which config to use. 你有三个配置和一个可执行文件的主要问题是你需要指定可执行文件使用哪个配置。

One option is to make 3 copies of your executable, exe1.exe , exe2.exe and exe3.exe and have a similarly named config for each - exe1.exe.config , exe2.exe.config and exe3.exe.config . 一个选项是制作3个可执行文件的副本, exe1.exeexe2.exeexe3.exe并为每个 - exe1.exe.configexe2.exe.configexe3.exe.config配置一个类似命名的配置。

When running each executable, it will use the correct config. 运行每个可执行文件时,它将使用正确的配置。

Another option is to have several batch files that will rename the different config files according to which one you want to use. 另一个选项是有几个批处理文件,根据您要使用的文件重命名不同的配置文件。 Then you have a single exe and three configs. 然后你有一个exe和三个配置。

You create a second executable, and always run that one first. 您创建第二个可执行文件,并始终先运行该可执行文件。 In it, all you do is rename one configfile to the correct name and fire the main application. 在其中,您所做的就是将一个配置文件重命名为正确的名称并触发主应用程序。

string currentConfig = "application.exe.config";
string someOtherName = "firstconfig.config";
string configFileYouWant = "secondconfig.config";
string application = "application.exe";

File.Move(currentConfig, someOtherName);
File.Move(configFileYouWant, currentConfig);
Process.Start(application);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM