vb.net 来自文本框输入的 Visual Basic CMD 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23767677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:34:32  来源:igfitidea点击:

Visual Basic CMD command from textbox input

vb.netshellcommand-prompt

提问by 21CmOfPain

I want to try something,this isn't the official project. So the users adds the webpage and lets say we name it %1 the code take that and the output is "ping %1" and press enter key.

我想尝试一些东西,这不是官方项目。因此,用户添加了网页并假设我们将其命名为 %1,代码采用该方法,输出为“ping %1”,然后按 Enter 键。

What I've done so far

到目前为止我所做的

Public Class Form3


    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim textvar As String = TextBox1.Text
        Shell("CMD.exe")
        SendKeys.Send("ping") 'textvar') '{ENTER}')

    End Sub
End Class

The output of that is an empty cmd window or crash.Any idea on how to fix that?

输出是一个空的 cmd 窗口或崩溃。关于如何解决这个问题的任何想法?

Update: Also i tried

更新:我也试过

SendKeys.Send("ping") 'textvar')
SendKeys.Send("{ENTER}")

but the output is 3 cmd windows 1 of them empty the other 2 split the "ping" word

但输出是 3 个 cmd 窗口,其中 1 个为空,另外 2 个拆分“ping”字

回答by aphoria

Instead of trying to send keystrokes (which can be problematic), just call pingas a parameter to CMD.

不要尝试发送击键(这可能有问题),只需将其ping作为参数调用CMD.

Shell("CMD.EXE /K PING " & textvar)

The /Kparameter keeps the CMDwindow open after the PINGcommand finishes.

/K参数CMDPING命令完成后保持窗口打开。

回答by Creator

Dim textvar As String = TextBox1.Text
Dim Command As String
Command = "Ping" & " " & textvar
Shell("cmd /k" & Command, 1, True)

This should be right

这应该是对的