how to Invoke a C# console application.

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?
c kAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽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.
1
Chinmay PatelChief Technical NinjaCommented:
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.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
c kAuthor 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.
0
Chinmay PatelChief Technical NinjaCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.