how to Invoke a C# console application.

mikha
mikha used Ask the Experts™
on
can a C# console application be called from another .net application.
One way, I know, is to call via windows scheduler.
are there any other ways?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
.NET assemblies can all be added as references within other applications. You could add a reference to the 2nd console app, and then just manually invoke Program.Main.
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi,

There are couple of things you can use based on your requirements
1. You can use System.Diagnostic.Process https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=netframework-4.7.1

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        public static void Main()
        {
            Process myProcess = new Process();

            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                // You can start any process, HelloWorld is a do-nothing example.
                myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
                // This code assumes the process you are starting will terminate itself. 
                // Given that is is started without a window so you cannot terminate it 
                // on the desktop, it must terminate itself or you can do it programmatically
                // from this application using the Kill method.
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

Open in new window


2. You can use Reflection to call the application - if you have full path to it.
3. As you have mentioned Scheduler, you could also put it in Windows Start-up folders - both user and machine level.

If you can clarify your scenario a bit more I think we can zero-in on a perfect solution.


Regards,
Chinmay.

Author

Commented:
thank you both @käµfm³d  and @Chinmay Patel.

@käµfm³d - do you have an example you can point me to.
@Chinmay - this question is out of curosity, sometimes I see lot of console applications in an solution. but don't necessarily see how they are being called. so figured there must be more than one way to call these.
can you provide any use case scenarios, as to , when one method would be used vs another, from the list you have provided.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018
Commented:
Sure.

System.Diagnostic is a very common way to call ANY (does not matter in which programming language it was created or whether you have access to / or even understanding of internals of the app) i.e. you could launch Microsoft Word, Counter Strike, A batch file you have created, or maybe a PDF file (given you have Adobe Acrobat or some other PDF handler installed on your target system). So not only executables, any valid file that has file association(It is a fancy word but in a simpler terms it means which file will be opened by which app) configured in windows can be launched using System.Diagnostics. I strongly recommend that you read the accompanying link as it will explain what else you can do - You can pass on parameters, control priority, terminate the app, etc. - using System.Diagnostics.

System.Reflection is used when you are dealing with an app/assembly built on .Net framework and you want to programmatically control the launch and then behavior (If the app has public methods/properties) you can call them from your code and control the behavior of the app during the runtime. It is bit complex but provides greater flexibility.

Windows Startup is a no code approach. You put a shortcut to your app in Windows Start folder and when Windows start your app will be launched.

As long as use cases are concerned, I have explained what each method does because it will be the business requirement that will drive your method of choice not other way around. I hope this explanation helps.

Regards,
Chinmay.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial