How to run an executable program with a different configuration file?

I want to run a program with a different configuration file, the program writes with C # 2.0, I make a different file name {program_name} .exe.config, I mean one exe with another configuration file, for example, I have 3 config file, then I will run 3 exe with a different configuration file, bu - the exe file is the same. Can I not modify the program to read another configuration file (I do not want to put the configuration file path in the exe command parameters) for this (for example, use a batch file or another method.)?

Thank.

+5
source share
4 answers

, , .

, 3 , exe1.exe, exe2.exe exe3.exe - exe1.exe.config, exe2.exe.config exe3.exe.config.

.

- , , . exe .

+1

, exe. SetData AppDomain. , .

1 exe.config 3 .

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


app.config

exe , , . exe SetData, .

+8

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);
                    }
                }
            }
        }
    }
+1

You create a second executable file and always run it first. In it, everything you do renames one configuration file to the correct name and launches 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);
0
source

All Articles