【问题标题】:How do you add command line switches to VB.NET WinForms application (when run from CMD)?如何将命令行开关添加到 VB.NET WinForms 应用程序(从 CMD 运行时)?
【发布时间】:2020-12-22 11:16:34
【问题描述】:

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

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

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

【问题讨论】:

  • 什么不起作用?你能展示你尝试过的东西吗?你检查过Environment.CommandLine吗?您是否在 Visual Studio IDE 中对此进行测试?您是否尝试过使用Project -> Properties -> Debug -> Command Line Arguments 工具?
  • 是的,我尝试了Environment.CommandLine,但开关不起作用。 For each args 方法也不起作用。
  • 如果它不起作用,那么你做错了。如果你不向我们展示你做了什么,我们就看不到你做错了什么。总是发布相关代码。
  • 您正在寻找Application.Startup() 事件。请参阅my example here 了解如何找到它。从备注部分:“您可以使用 e 参数的 Cancel 属性来控制应用程序启动表单的加载。当 Cancel 属性设置为 True 时,启动表单不会启动。在这种情况下,您的代码应该调用备用启动代码路径。您可以使用 e 参数的 CommandLine 属性或 CommandLineArgs 属性来访问应用程序的命令行参数。"

标签: vb.net command-line-arguments


【解决方案1】:

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

然后在Form_Load事件中:

' 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 应用程序。

【讨论】:

    猜你喜欢
    • 2013-08-23
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 2016-06-17
    • 2011-11-05
    • 1970-01-01
    相关资源
    最近更新 更多