Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

command line to make winforms run without UI

Tags:

c#

winforms

i am not really sure how to ask this question properly. so apologies in advance.

but it's basically around how to make an existing app, with a UI, run as a scheduled task with no UI.

background..

I have a winforms app written in vs2012 with 2 forms.

the first form is the login, straight forward, but currently expects user interaction of their username and password.

the second is the main form. which does the main work when you hit the "start" button.

what I am trying to achieve it is for me to send it some command line parameters that would run it without any ui as a scheduled task.

so, I'm guessing, I need get rid of needing the user to input login details. and somehow trigger the "start download" button and make it invisible.

I've worked out how to send command line parameters and can see how to get the software to do something different if it hears /silent but how do I hide the forms?

I'm lost.

any help would be much appreciated!

like image 332
Trevor Daniel Avatar asked Oct 19 '25 07:10

Trevor Daniel


2 Answers

C# still has a Main() function. In a standard winforms solution, all it does is create your Form1 (or whatever it gets renamed to), and start the application event queue with that form.

For example, it should look something like:

public static void Main()
{
     Application.Run(new Form1());
}

Modify this function. If you see command line arguments, do whatever you have to. If there are none, let it do its normal magic with the form. When you're done, it would be something like:

public static void Main(string[] args)
{
    if (args.Length > 0) {
        // log in
        // do all the necessary stuff
    } else {
        Application.Run(new Form1());
    }
}
like image 178
Scott Mermelstein Avatar answered Oct 21 '25 22:10

Scott Mermelstein


Modify the Main method which should be the entry point to your application. If there are any arguments, you don't need to instantiate and show any forms, just run the code to do your job without UI.

    [STAThread]
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            // silent mode - you don't need to instantiate any forms
        }
        else
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
like image 30
Szymon Avatar answered Oct 21 '25 21:10

Szymon