C# - 是否有可能创建一个Windows窗体,可以从参数从命令行运行应用程序?(C# - Is i

2019-08-17 07:40发布

我想Windows窗体应用程序将包含一个用户界面,但我希望它从一些参数的命令行,也可能是一个运行/hide/visible=false选项。

怎么可能在命令行参数读? 并相应调整?

Answer 1:

如果您更改此默认主要特征:

[STAThread]
static void Main()

为此:

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

您可以访问命令行变量,当你从一个正常的控制台应用程序会,或者如果你想从访问它们在其他地方,你可以使用:

System.Environment.GetCommandLineArgs();


Answer 2:

[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));
    }
}


Answer 3:

使用Environment.GetCommandLineArgs()



Answer 4:

是的,它应该工作,以创建项目作为一个正常的Windows应用程序项目。 然后,在你的Program.cs启动窗口前,调用Environment.GetCommandLineArgs()得到命令行参数,并分析它们做你想做什么。



文章来源: C# - Is it possible to create a Windows Forms application that can run from the command line with parameters?