How can I access and run a console application from a windows form, which is part of the same project. I have a windows form and a console application. I think that I can publish the console application and then use Process.Start(path to console app) but this is not what I want. I want to access and use the Main method of the console application in my form project. This method would run at a click on a button.
This gives the following error.
InvalidOperationException was unhandled
Cannot read keys when either application does not have a console
or when console input has been redirected from a file. Try Console.Read.
private void buttonApplication_Click(object sender, EventArgs e)
{
Ch15Extra_2.Program.Main();
}
Here are the methods.
ConsoleApp:
namespace Ch15Extra_2
{
public class Program
{
public static void Main()
{
Console.WriteLine("Here the app is running");
Console.ReadKey();
}
}
}
Form1:
private void buttonApplication_Click(object sender, EventArgs e) { }
If you need to run your console app without WinForms app, and sometimes you want to execute some console code without running a console app, I have a suggestion for you:
You can divide your solution into three parts.
Link a dll to first and to the second projects.
Then if you need to run shared code from WinFomrs app, you can do:
SharedClasswill be implemented in the third project.You can call it from console app too.
upd
Project 1 : ClassLibrary.
Proj 2. WinForms. Has a Reference to Project 1
using Shared;
proj 3. Console App
I’ve just checked – it works.