gpt4 book ai didi

vb.net - 知道程序是从命令行启动还是在 vb.net 中单击 .exe?

转载 作者:行者123 更新时间:2023-12-02 00:42:54 25 4
gpt4 key购买 nike

您好,我正在用 vb.net 编写程序。该程序可以通过传递一些参数从另一个程序启动,也可以通过单击 .exe 启动。我想根据用户的去向向用户展示一些选项。以下方法是否正确?

Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Environment.GetCommandLineArgs(0).ToString = "SomeArgument" Then
'Do some events
Else
'Do some other events
End If

End Sub

感谢您的帮助。

最佳答案

看起来您走在正确的轨道上,但是您应该检查第一个位置之后的参数。来自GetCommandLineArgs documentation :

The first element in the array contains the file name of the executing program. If the file name is not available, the first element is equal to String.Empty. The remaining elements contain any additional tokens entered on the command line.

您的代码正在检查数组的第一个元素,因此它将是程序的名称。根据您希望传入的参数数量,您应该遍历它并确定它是否存在。

For Each arg As String In Environment.GetCommandLineArgs()
If arg = "SomeArg" Then
' do something
End If
Next

' LINQ approach
If Environment.GetCommandLineArgs().Any(Function(arg) arg = "SomeArg") Then
' do something
End If

此外,它是一个字符串数组,因此无需在元素上使用 ToString()

关于vb.net - 知道程序是从命令行启动还是在 vb.net 中单击 .exe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2031062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com