I need to be able to run a Windows Forms function or event (for example a button click) from a command line. How could I do this?
I can't use a console or windows service due to certain constraints.
I need to be able to run a Windows Forms function or event (for example a button click) from a command line. How could I do this?
I can't use a console or windows service due to certain constraints.
If you mean re-use a method in your Windows Forms application in a command line (Console) application, then simply reference the assembly containing the class and call that method, a bit like this
static void Main()
{
Form1 form = new Form1();
form.SomeButton_Click();
}
Note that for this to work the SomeButton_Click
method and the Form1
class must be public. If this is what you want to do then this is a good indication that the logic contained in SomeButton_Click
should be refactored to some common utility class.
If alternatively you want to simulate a button click in a running Windows Forms application then you have a couple of options
There are many different ways to achieve either of these however both are more complex than just calling a method - it would help if you provided more detail on your problem.
You may be able to do this with a Powershell script: UI Automation with Windows PowerShell