简体   繁体   中英

.Net multiple console apps projects in a solution to be a referenced in one console app project

I have a need to have multiple console apps

For example, App1 to perform task A, App2 to perform task B, App3 to perform task C

Now,

1) I need App1 to be able to execute App2 and App3 optionally, which is controlled based on configuration flags..

2) App2 and App3 should be independently executable.

Update: I can get App1 to execute App2 and App3 optionally, the major problem is that App2 and App3 has its own configs and they don't get generated in App1's bin folder, so when i execute App2 or App3 from there, it complains of the missing configs.

Please let me know the best approach to achieve this. Please let me know if u need more information.

Your best option would be to put your shared functionality into a library project. Reference this library project in the console applications where the functionality is needed.

Create a new solution with the three console applications as projects. Define App2 and App3 as required. Then, for App3 add the other applications as references. Now, in the code for App3, you can call the other applications' main methods as desired. This works. But, please note that it is a little strange to add an application as a reference.

Normally, you should have a single executable referencing other dll's.

What you can do is, create a class library (dll) project. Code all 3 functionality in this project. Then, create 3 separate console applications which all reference this shared dll and use the various functionalities provided by your shared dll as required by configuration and input parameters.

Without knowing more the only way I could really suggest, is that you have app1.exe run and then depending on the configuration flags use either a case statment to determine the executable to run:

int configFlag = 1;
switch (configFlaf)
{
    case 1:
        process.start("app2.exe");
        break;
    case 2:
        Console.WriteLine("app3.exe");
        break;
    default:
        Console.WriteLine("Invalid Config Flag");
        break;
}

or you could use an if statement:

if(configFlag1 && configFlag2) 
   process.start("app2.exe")
else
   process.start("app3.exe");

If you provide more information I'll gladly put some more information and detail into this explanation.

Expansion on your update 1 information The only way I can see around getting around the missing config issues (especially if the executables aren't going to be in the same folder when deployed) would be to launch app2.exe and app3.exe with an argument of either:

  1. The directory of the configuration files.
  2. The working directory of app1.exe

So you would launch app2.exe like so:

process.start("app2.exe", "c:\config\");

or

process.start("app2.exe", Directory.GetCurrentDirectory());

Then in app2.exe:

class app2.
{
    static void Main(string[] args)
    {
        if (args.Length > 0"))
            Environment.CurrentDirectory = (args[0]);
    }
}

You can find more information on using command line arguments here: https://msdn.microsoft.com/en-us/library/aa288457%28v=vs.71%29.aspx

The solution to my problem was quite simple. All I had to do was,

copy App2.config and App3.config to App1's bin/debug folder..

and it worked like a charm!! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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