2015-05-20 53 views
0

我正在編寫一個小型自動化程序,它將作爲計劃任務運行。我希望能夠將命令行參數傳遞給它。我有參數部分工作,但如果我運行它與任何參數,沒有任何反應。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 
+1

你的arent顯示相關的代碼你using.have嘗試過,所以很難說有什麼我們看不到的心不是工作 – Plutonix

回答

0

一個模塊中創建一個Main方法,以及該項目的啓動設置爲Sub Main,定義字符串數組將包含命令行參數。

然後檢查數組

Sub Main(ByVal args() As String) 

    If args.Length > 0 Then 

    End If 

End Sub 
+0

最後我使用的這個變化通過使用Dim Args = My.Application.CommandLineArgs.ToArray() 如果Args.Length> 0 Then ...' –

+0

CommandLineArgs是一個List,所以.Count屬性起作用。您不需要轉換爲數組來獲取長度。 – Jeremy

1

我寫了一個輔助方法,我沿着時間前的長度,其提取馬桶蓋或返回的值,如果存在或不 - 這將幫助你也許。

/// <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 
     } 
     } 
相關問題