我正在編寫一個小型自動化程序,它將作爲計劃任務運行。我希望能夠將命令行參數傳遞給它。我有參數部分工作,但如果我運行它與任何參數,沒有任何反應。vb.net 2010控制檯應用程序命令行aguments
我使用
Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs`
與
For i As Integer = 0 To CommandLineArgs.Count - 1
Dim arg As String = ""
arg = CommandLineArgs(i)`
然後沿,基於傳遞的說法,我有一個如果/那麼語句perfom任務。如果沒有參數傳遞,或者要顯示帶有使用語法的消息,我希望它能夠運行預定義的任務。
如何在嘗試解析參數之前檢查參數是否已通過?
添加完整的代碼...
Imports System.Windows.Forms
Imports System.IO
Module Module1
Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs
Sub Main()
Dim strfilename As String
Dim num_rows As Long
Dim num_cols As Long
Dim x As Integer
Dim y As Integer
Dim strarray(1, 1) As String
'Load the file
strfilename = "library.csv"
'Check if file exist
If File.Exists(strfilename) Then
Dim tmpstream As StreamReader = File.OpenText(strfilename)
Dim strlines() As String
Dim strline() As String
'Load content of file to strLines array
strlines = tmpstream.ReadToEnd().Split(Environment.NewLine)
' Redimension the array.
num_rows = UBound(strlines)
strline = strlines(0).Split(",")
num_cols = UBound(strline)
ReDim strarray(num_rows, num_cols)
' Copy the data into the array.
For x = 1 To (num_rows - 1)
strline = strlines(x).Split(",")
For y = 0 To num_cols
strarray(x, y) = strline(y)
Next
Next
End If
For i As Integer = 0 To CommandLineArgs.Count - 1
Dim arg As String = ""
arg = CommandLineArgs(i)
If arg.ToLower() = "/setup" Then
Dim form As New frmAdmin
Try
System.Windows.Forms.Application.Run(form)
Catch ex As Exception
'add logging
End Try
ElseIf arg.ToLower() = "/destiny" Then
'argument test
MessageBox.Show(CommandLineArgs(i))
Try
'code
Catch ex As Exception
'logging
End Try
ElseIf arg.ToLower() = "/ic" Then
'argument test
MessageBox.Show(CommandLineArgs(i))
Try
'code
Catch ex As Exception
'logging
End Try
ElseIf arg.ToLower() = "/adadd" Then
'argument test
MessageBox.Show(CommandLineArgs(i))
Try
'code
Catch ex As Exception
'logging
End Try
ElseIf arg.ToLower() = "/adremove" Then
'argument test
MessageBox.Show(CommandLineArgs(i))
Try
'code
Catch ex As Exception
'logging
End Try
ElseIf arg.ToLower() = "/help" Then
'argument test
MessageBox.Show(CommandLineArgs(i))
Try
'code
Catch ex As Exception
'logging
End Try
ElseIf arg.ToLower() = "/automate" Then
'argument test
MessageBox.Show(CommandLineArgs(i))
Try
'code
Catch ex As Exception
'logging
End Try
Else
MessageBox.Show("Please use /help to see usage")
End If
Next
End Sub
End Module
你的arent顯示相關的代碼你using.have嘗試過,所以很難說有什麼我們看不到的心不是工作 – Plutonix