如何运行和隐藏CMD命令

时间:2018-07-24 17:12:39

标签: vb.net

嘿,我正在Visual Basic(VB.NET)中进行编程,并且我尝试运行cmd命令而不在计算机上显示cmd屏幕,但是我有以下代码,但是我无法将其隐藏。 / p>

代码:

Dim CMD As New Process
CMD.StartInfo.FileName = "cmd.exe"
CMD.StartInfo.UseShellExecute = False
CMD.StartInfo.RedirectStandardInput = True
CMD.StartInfo.RedirectStandardOutput = True
CMD.StartInfo.CreateNoWindow = True
CMD.Start()
Dim SW As System.IO.StreamWriter = CMD.StandardInput
Dim SR As System.IO.StreamReader = CMD.StandardOutput
SW.WriteLine("exit")
Process.Start("Cmd.exe", "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt")
Thread.Sleep(5000)

1 个答案:

答案 0 :(得分:-1)

除了丑陋的代码,您基本上可以这样做:

    Dim CMD As New Process
    CMD.StartInfo.FileName = "cmd.exe"
    CMD.StartInfo.UseShellExecute = False
    CMD.StartInfo.RedirectStandardInput = True
    CMD.StartInfo.RedirectStandardOutput = True
    CMD.StartInfo.CreateNoWindow = True
    CMD.Start()
    Dim SW As System.IO.StreamWriter = CMD.StandardInput
    Dim SR As System.IO.StreamReader = CMD.StandardOutput
    SW.WriteLine("exit")

    Dim p2 As New Process
    p2.StartInfo.FileName = "Cmd.exe"
    p2.StartInfo.Arguments = "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt"
    p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    p2.Start()

    Thread.Sleep(5000)

Process.Start是一个静态方法,它具有一组有限的参数来修改其行为。

因此,您可以通过静态方法指定.exe的名称和参数

 Process.Start("Cmd.exe", "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt")

,类似于:

    Dim p2 As New Process

    p2.StartInfo.FileName = "Cmd.exe"
    p2.StartInfo.Arguments = "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt",

    p2.Start()

但是通过这样做,您可以使用更多选项,包括将WindowStyle设置为隐藏的选项:

p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden