如何运行CMD命令并将其隐藏?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (81)

我正在用VisualBasic(VB.NET)编程,试图运行cmd命令,然而却没有在计算机上显示cmd屏幕。

代码:

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)
提问于
用户回答回答于

你基本上可以这样做:

    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)

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

扫码关注云+社区

领取腾讯云代金券