Posted By

sekihin on 06/17/09


Tagged

VBNet


Versions (?)

获得启动参数


 / Published in: VB.NET
 

  1. Public Function ParseCommandLineArgs(ByVal args As String()) As Hashtable
  2. Dim argsTable As New Hashtable()
  3.  
  4. If (args Is Nothing) OrElse (args.Length = 0) Then
  5. Return argsTable
  6. End If
  7.  
  8. Dim arg As String
  9. Dim key As String
  10. Dim val As String
  11. Dim pos As Integer
  12. Dim tempVal As System.Text.StringBuilder
  13. Dim preChar As String = ""
  14.  
  15. For i As Integer = 0 To args.Length - 1
  16. arg = args(i)
  17.  
  18. pos = arg.IndexOf("=")
  19.  
  20. If pos > 0 Then
  21. key = arg.Substring(0, pos)
  22. pos = pos + 1
  23. val = arg.Substring(pos, arg.Length - pos)
  24. ' 両側のダブルクォーテーションをはずす
  25. If (val.Length > 2) AndAlso (val.StartsWith("""") = True) AndAlso (val.EndsWith("""") = True) Then
  26. val = val.Substring(1, val.Length - 2)
  27. End If
  28.  
  29. tempVal = New System.Text.StringBuilder()
  30. preChar = ""
  31. For j As Integer = 0 To val.Length - 1
  32. If preChar = "\" Then
  33. If val.Substring(j, 1) = "\" Then
  34. ' \を発見
  35. tempVal.Append("\")
  36. preChar = ""
  37. ElseIf val.Substring(j, 1) = """" Then
  38. ' "を発見
  39. tempVal.Append("""")
  40. preChar = """"
  41. Else
  42. tempVal.Append("\")
  43. preChar = val.Substring(j, 1)
  44. tempVal.Append(preChar)
  45. End If
  46. Else
  47. If val.Substring(j, 1) <> "\" Then
  48. tempVal.Append(val.Substring(j, 1))
  49. End If
  50. preChar = val.Substring(j, 1)
  51. End If
  52. Next
  53. val = tempVal.ToString()
  54. Else
  55. key = arg
  56. val = ""
  57. End If
  58.  
  59. ' キーを大文字にする
  60. key = key.ToUpper()
  61.  
  62. If argsTable.Contains(key) = False Then
  63. ' キーが存在しないとき
  64. argsTable.Add(key, val)
  65. End If
  66. Next
  67.  
  68. Return argsTable
  69. End Function
  70.  
  71. Private Function GetCommandLine1(ByVal args() As String) As Boolean
  72. 'コマンドラインの引数を受け取る
  73. Dim _ArgsTable As New Hashtable
  74. _ArgsTable = ParseCommandLineArgs(args)
  75.  
  76. If _ArgsTable IsNot Nothing AndAlso _
  77. _ArgsTable.Keys.Count > 0 AndAlso _
  78. _ArgsTable.Contains("KEY") = True Then
  79. Return String.IsNullOrEmpty(_ArgsTable("KEY").ToString())
  80. Else
  81. Return False
  82. End If
  83.  
  84. End Function

Report this snippet  

You need to login to post a comment.