试试这个:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
commands = New Dictionary(Of String, Func(Of String, IEnumerable(Of String), String))() From _
{ _
{"getip", Function(c, ps) Me.GetIP().ToString()}, _
{"nop", Function(c, ps) "No Command"} _
}
End Sub
Private commands As Dictionary(Of String, Func(Of String, IEnumerable(Of String), String)) = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim command = {"nop"}.Concat(Me.RichTextBox1.Lines) _
.Reverse().First(Function(l) Not [String].IsNullOrWhiteSpace(l))
If (If(Me.RichTextBox1.Lines.LastOrDefault(), "")).Length > 0 Then
Me.RichTextBox1.AppendText(Environment.NewLine)
End If
Dim parts = command.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
Dim result = "Invalid Command"
If commands.ContainsKey(parts(0).ToLower()) Then
result = commands(parts(0))(parts(0), parts.Skip(1).ToArray())
End If
Me.RichTextBox1.AppendText("SYS $~ " + result)
End Sub
Private Function GetIP() As String
Dim host As IPHostEntry
Dim localIP As String = "?"
host = Dns.GetHostEntry(Dns.GetHostName())
For Each ip As IPAddress In host.AddressList
If ip.AddressFamily = AddressFamily.InterNetwork Then
localIP = ip.ToString()
End If
Next
Return localIP
End Function
你需要添加以下的进口:
Imports System.Net
Imports System.Net.Sockets
然后,你可以打造出来的字典为每个命令。
欢迎来到Stack Overflow!请不要只是要求我们为您解决问题。告诉我们你是如何试图自己解决问题的,然后向我们展示结果是什么,并告诉我们为什么你觉得它不起作用。请参阅“[您尝试过什么?](http://whathaveyoutried.com/)”,以获得一篇您最近需要阅读的优秀文章。 –
是的,我知道,但我尝试了很多方法来实现它,但它没有奏效。 – Shahar
“我尝试了很多方法来制作它”发布你尝试过的一些方法 –