从cmd命令中拾取字符串? Process.StartInfo

时间:2010-01-29 13:09:06

标签: vb.net cmd

所以我尝试过Process并启动cmd.exe并将命令直接发送到该窗口。然后选取写入cmd.exe窗口的值。

代码如下所示:

Dim arrServers As ArrayList
    Dim s(ListBoxServers.Items.Count) As String

    ListBoxServers.Items.CopyTo(s, 0)
    arrServers = New ArrayList(s)

    Using P As New Process
        P.StartInfo.FileName = "cmd.exe"
        P.StartInfo.UseShellExecute = False
        P.StartInfo.RedirectStandardOutput = True
        P.StartInfo.RedirectStandardInput = True
        P.Start()
        For Each i In arrServers
            P.StandardInput.WriteLine("query user " & txtBoxUsername.Text & " /server:" & i)
        Next
        P.StandardInput.WriteLine("exit")
        Output = P.StandardOutput.ReadToEnd()
        Trace.WriteLine(Output)
        MsgBox(Output)
        P.WaitForExit()

    End Using

但看起来它并没有“按回车”或其他东西。意思是,我没有从命令中得到任何结果。我甚至没有得到“'命令'不被识别为内部或外部命令,可操作程序或批处理文件。”就像你通常得到的,如果它不理解语法。

3 个答案:

答案 0 :(得分:2)

查看System.Diagnostics命名空间中的Process类以运行批处理文件。

答案 1 :(得分:1)

使用类似NDesk's Options的库/类来进行灵活的参数处理。如果您不想使用外部组件,则必须循环遍历参数并手动处理它们:

For Each arg As String In Environment.GetCommandLineArgs()
  Select Case arg
    Case "/blah"
      ' process /blah '
    Case "/foo"
      ' process foo '
    Case Else
      MsgBox "Unknown argument " + arg " found, aborting.", vbCritical
      Environment.Exit(1)
  End Select
Next

[我通常不做VB,所以这只是一个未经测试的草图]

答案 2 :(得分:1)

想象一下名为“hello.bat”的以下非常简单的批处理文件

@ECHO OFF
echo Hello

您可以使用以下方式调用它并查看“Hello”:

    'Will hold the results of the batch
    Dim Output As String
    'Create a new process object
    Using P As New Process()
        'Set the script to run
        P.StartInfo.FileName = "c:\scripts\hello.bat"
        'My script doesn't take argument but this is where you would pass them
        P.StartInfo.Arguments = ""
        'Required to redirect output, don't both worrying what it means
        P.StartInfo.UseShellExecute = False
        'Tell the system that you want to see the output
        P.StartInfo.RedirectStandardOutput = True
        'Start your batch
        P.Start()
        'Read the entire contents of the outout
        Output = P.StandardOutput.ReadToEnd()
        'Wait until the batch is done running
        P.WaitForExit()
    End Using
    'Do something with the output
    Trace.WriteLine("Batch produced : " & Output)

修改

这是一个不运行批处理但是运行几个标准命令的版本。我们首先启动一个命令shell来传递内容。糟糕的是,它难以运行命令,读取输出然后运行另一个命令。下面的代码背靠背地运行两个命令,并将整个结果转储到字符串中。如果您需要运行命令,处理,运行另一个命令,我认为您必须将某些内容连接到StandardError并查看返回代码。在您这样做之前,请确保通过连接线程as here来了解阻塞问题以及其他地方如何解决阻塞问题。可能更简单的方法是将其包装到sub中并为每个命令调用sub。

    'Will hold all of the text
    Dim Output As String
    'Create a new process object
    Using P As New Process()
        'Set the script to run the standard command shell
        P.StartInfo.FileName = "cmd.exe"
        'Required to redirect output, don't both worrying what it means
        P.StartInfo.UseShellExecute = False
        'Tell the system that you want to read/write to it
        P.StartInfo.RedirectStandardOutput = True
        P.StartInfo.RedirectStandardInput = True
        'Start your batch
        P.Start()
        'Send your various commands
        P.StandardInput.WriteLine("dir c:\")
        P.StandardInput.WriteLine("ipconfig /all")
        'Very important, send the "exit" command otherwise STDOUT will never close the stream
        P.StandardInput.WriteLine("exit")
        'Read the entire stream
        Output = P.StandardOutput.ReadToEnd()
        'Wait until the batch is done running
        P.WaitForExit()
    End Using
    'Do something with the output
    Trace.WriteLine(Output)

修改2

我一般遇到“查询用户”命令的问题,即使我将名字括在引号中,我也无法让它返回任何包含空格的用户名。但是这里有一个使用“quser”的版本,就我所知,它完全相同。

    'Will hold all of the text
    Dim Output As String
    'Create a new process object
    Using P As New Process()
        'Set the script to run the standard command shell
        P.StartInfo.FileName = "cmd.exe"
        'Required to redirect output, don't both worrying what it means
        P.StartInfo.UseShellExecute = False
        'Tell the system that you want to read/write to it
        P.StartInfo.RedirectStandardOutput = True
        P.StartInfo.RedirectStandardInput = True
        'Start your batch
        P.Start()
        'Send your various commands
        'Array of servers
        Dim arrServers() As String = New String() {"SERVER1", "SERVER2"}
        'Loop through array, wrap names with quotes in case they have spaces
        For Each S In arrServers
            P.StandardInput.WriteLine(String.Format("quser ""{0}"" /SERVER:{1}", Me.txtBoxUsername.Text, S))
        Next
        'Very important, send the "exit" command otherwise STDOUT will never close the stream
        P.StandardInput.WriteLine("exit")
        'Read the entire stream
        Output = P.StandardOutput.ReadToEnd()
        'Wait until the batch is done running
        P.WaitForExit()
    End Using
    'Do something with the output
    Trace.WriteLine(Output)