C# - Is it possible to create a Windows Forms appl

2019-04-04 16:15发布

I would like a Windows Forms application that will contain a UI, but I want it to run from the command line with some parameters, possibly also a /hide or /visible=false option.

How is it possible to read in the command line parameters? And adjust accordingly?

4条回答
对你真心纯属浪费
3楼-- · 2019-04-04 16:37

If you change this default Main signature:

[STAThread]
static void Main()

To this:

[STAThread]
static void Main(String[] args)

You can access the commandline variables as you would from a normal console app, or if you want to access them from elsewhere you can use:

System.Environment.GetCommandLineArgs();
查看更多
够拽才男人
4楼-- · 2019-04-04 16:37
[STAThread]
static void Main(string[] args)
{
    if (args.Length == 0)
    {
        // Run the application in a windows form
        Application.Run(new MainForm( ));
    }
    else
    {
        // Run app from CLI
        Console.WriteLine(DoStuff(args));
    }
}
查看更多
放我归山
5楼-- · 2019-04-04 16:46

Yes, it should work to create the project as a normal windows app project. Then in program.cs before you launch the window, call Environment.GetCommandLineArgs() to get the command line arguments and parse them to do what you want.

查看更多
登录 后发表回答