Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I added an existing project from console application to my windows form project.I would like to know if is any way to call the console application main method from windows form.I have already add the 2 project to my solution(For example when i press a button.)tutorials,examples...

Thank you



C#
private void button1_Click(object sender, EventArgs e)
{
 //call main method



}


What I have tried:

I don t know if is it possible.I haven't found anything over the internet.
Posted
Updated 15-May-16 16:55pm
Comments
Sergey Alexandrovich Kryukov 15-May-16 23:18pm    
No, you haven't. There is no such concept as adding a project to a project. Probably, you added some project to the same solution.
You need to explain what you want to achieve via using your console application. Anyway, a different application means a separate project, so you cannot just call a method.
—SA
PIEBALDconsult 15-May-16 23:51pm    
Yes, but ideally you would write a separate API that both applications would use.

If your intention is to start a new process using an existing application from within the Windows Form, this solution might be right for you.

In that case you can use the Process class to start the other application.
See Process Class (System.Diagnostics)[^]

If you read the documentation you will find some pretty good examples.

If you want to transfer parameters at startup you should look into the ProcessStartInfo Class (System.Diagnostics)[^] and use the property Arguments.
The arguments should be separated with a space.

In your console application you use the args parameter of the main method.
C#
static void Main(string[] args)
{
    foreach (string arg in args)
    {
        // Do something
    }
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-May-16 23:22pm    
Strictly speaking, it won't call the method from another application. What you wrote would make sense... if the whole idea made any sense, but we don't know why the inquirer would need it. Most likely, it's some kind of misconception. I would try to understand what would be the ultimate goal of all this activity. In that respect, Solution 1 makes more sense.
—SA
George Jonsson 16-May-16 0:09am    
It was my interpretation of the question that the OP wants to start another process (.exe) from within a Windows Form.
But you are right, reading the question again makes me a bit confused, so maybe this is want the OP wants or maybe not. More info required.
George Jonsson 16-May-16 0:12am    
I rephrased my solution slightly.
I dont know why you'd call the 'main' method per se - if I had to do integrate functionality from a console program into a WinForms program I would

a) define a new static function within the Winforms Project eg xyz_main

b) copy the lines from the console main code line into new function xyz_main

c) have your button1_Click handler call xyz_main

d) get it working (fix references) etc then

e) refactor as required to make better use of a WinForms project - use textboxes on WinForms Form for results, input etc

Thats a rough guide - it really depends how involved the main() of the console project is of course
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-May-16 23:28pm    
I voted 4 this time.
The whole advice is far from the final solution, but you added a fair note on "rough guide". It would be useful to add some explanation that the initial inquirer's request is based on some kind of misconception and explain the drawback of having different processes for integration. I also don't like that you encourage the bad practice of using auto-generated names such as button1_Click. Such names should never be used, as they violate (good) Microsoft naming conventions and don't reflect semantics of the handler, which makes project poorly maintainable. Generally, it's bad to suggest that some method is a handler; instead, += operator should always be shown as an evidence that some method is an event handler.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900