If you need to run a console application without a WinForms application, and sometimes you want to execute some console code without starting a console application, I have a suggestion for you:
You can divide the solution into three parts.
- WinForms Part
- Console part
- Dll library.
Link the dll to the first and second projects.
Then, if you need to run the generic code from the WinFomrs application, you can do:
private void buttonApplication_Click(object sender, EventArgs e) { var shared = new SharedClass(); shared.Run(); }
SharedClass will be implemented in the third project. You can also call it from the console application.
update
Project 1: ClassLibrary.
public class SharedClass { public int DoLogic(int x) { return x*x; } }
Proj 2. WinForms. Has a link to the project 1
using Shared;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { TextBox textBox = new TextBox(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var shared = new SharedClass(); textBox.Text = shared.DoLogic(10).ToString(); } } }
proj 3. Console application
public class Program { public static void Main() { Console.WriteLine("Here the app is running"); var shared = new Shared.SharedClass(); Console.WriteLine(shared.DoLogic(10)); Console.ReadKey(); } }
I just checked - it works.
source share