尝试使用RichTextBox命令创建像“Shell”这样的命令

时间:2015-06-03 00:12:34

标签: vb.net shell terminal

尝试使用RichTextBox命令创建Shell之类的命令。

任何人都可以通过简单的方式制作它吗? 我在整个网站上搜索这个都找不到办法。 所以它应该像任何终端/ shell /控制台应用程序 所以我可以简单地添加自定义命令,如whatismyip,它显示我的IP地址等。

应该像this

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
  For I As Integer = 0 To RichTextBox1.Lines.Count - 1 
    Dim Parts() As String = RichTextBox1.Lines(I).Split 
    Select Case Parts(0).ToUpper 
      Case "getip" 
        RichTextBox1.AppendText("SYS $~ 127.0.0.1") 
      Case Else 
        RichTextBox1.AppendText("SYS $~ Invalid command.") 
    End Select 
  Next 
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

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

然后你可以为每个命令构建字典。