我正在编写一个小型自动化程序,它将作为计划任务运行。我希望能够将命令行参数传递给它。我有参数部分工作,但如果我没有任何参数运行它,没有任何反应。
我正在使用
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)`
然后,根据传递的参数,我有一个If / Then语句来执行任务。如果没有传递参数,我希望它运行预定的任务,或者使用使用语法呈现消息。
如何在尝试解析参数之前检查是否传递了参数?
添加完整代码......
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
答案 0 :(得分:1)
我已经为我写了一个帮助方法,它提取了swtich的值或返回(如果存在或不存在) - 这对你有帮助。
/// <summary>
/// If the arguments are in the format /member=value
/// Than this function returns the value by the given membername (!casesensitive) (pass membername without '/')
/// If the member is a switch without a value and the switch is preset the given ArgName will be returned, so if switch is presetargname means true..
/// </summary>
/// <param name="args">Console-Arg-Array</param>
/// <param name="ArgName">Case insensitive argname without /</param>
/// <returns></returns>
private static string getArgValue(string[] args, string ArgName)
{
var singleFound = args.Where(w => w.ToLower() == "/" + ArgName.ToLower()).FirstOrDefault();
if (singleFound != null)
return ArgName;
var arg = args.Where(w => w.ToLower().StartsWith("/" + ArgName.ToLower() + "=")).FirstOrDefault();
if (arg == null)
return null;
else
return arg.Split('=')[1];
}
示例强>
static void Main(string[] args)
{
var modeSwitchValue = getArgValue(args, "mode");
if (modeSwitchValue == null)
{
//Argument not present
return;
}
else
{
//do something
}
}
答案 1 :(得分:0)
在模块中创建Main方法,并将项目启动设置为Sub Main,定义将包含命令行参数的字符串数组。
然后检查数组的长度
Sub Main(ByVal args() As String)
If args.Length > 0 Then
End If
End Sub