如何将命令行开关添加到VB.NET WinForms应用程序(从CMD运行时)?

如何将命令行开关添加到VB.NET WinForms应用程序(从CMD运行时)?,vb.net,command-line-arguments,Vb.net,Command Line Arguments,我正在创建一个基于GUI的应用程序(VB.NET);我希望它在从命令行运行时具有开关(例如-s,-r,-a,等等),而不显示GUI 我已经尝试了很多方法,但都不起作用,即使是微软自己的方法 请帮帮我。我已经研究了一天了。将您的启动表单设置为.visible=false 然后在表单加载事件中: ' Loop through the passed arguments For Each argument As String In My.Application.CommandLineArgs ' D

我正在创建一个基于GUI的应用程序(VB.NET);我希望它在从命令行运行时具有开关(例如
-s
-r
-a
,等等),而不显示GUI

我已经尝试了很多方法,但都不起作用,即使是微软自己的方法


请帮帮我。我已经研究了一天了。

将您的启动表单设置为
.visible=false

然后在
表单加载
事件中:

' Loop through the passed arguments
For Each argument As String In My.Application.CommandLineArgs

 ' Do stuff. i.e.:

 if argument = "-s" then
    ' Do something
 End if

 if argument = "-r" then
    ' Do something
 End if

 if argument = "-a" then
    ' Make the form visible
    Me.Visible = True
 End if

Next

此外,如果您打算从不使用GUI,只需制作一个控制台应用程序而不是winforms应用程序。

什么不起作用?你能展示一下你试过的吗?你查过了吗?您是否在VisualStudioIDE中测试此功能?您是否尝试过使用
Project->Properties->Debug->Command-Line Arguments
工具?是的,我尝试过
Environment.CommandLine
,但开关不起作用。每个args的
方法也不起作用。如果它不起作用,则说明您做错了。如果你不告诉我们你做了什么,我们就看不出你做错了什么。始终发布相关代码。您正在查找
Application.Startup()
事件。请参阅以了解如何找到它。从备注部分:“您可以使用e参数的Cancel属性来控制应用程序启动窗体的加载。当Cancel属性设置为True时,启动窗体不会启动。在这种情况下,代码应该调用备用启动代码路径。您可以使用e参数的CommandLine属性或CommandLineArgs属性访问应用程序的命令行参数。”