简单软件测试工具VB.NET

简单软件测试工具VB.NET,vb.net,Vb.net,好的,请不要嘲笑这个:x 我正在尝试在VB.NET中创建一个简单的软件测试工具 我创建了一个简单的C程序PROG.EXE,它扫描一个数字并打印输出,然后开始构建我的测试仪,它应该执行PROG.EXE OUTPUT.txt,因此PROG.EXE从input.txt获取输入并将输出打印到OUTPUT.txt 但我失败了,一开始我尝试了这个过程。然后启动shell,但没有任何效果 所以我做了这个把戏,VB.NET代码用这个代码生成了一个批处理文件PROG.EXE output.txt,但是我还是失败了

好的,请不要嘲笑这个:x
我正在尝试在VB.NET中创建一个简单的软件测试工具
我创建了一个简单的C程序PROG.EXE,它扫描一个数字并打印输出,然后开始构建我的测试仪,它应该执行PROG.EXE OUTPUT.txt,因此PROG.EXE从input.txt获取输入并将输出打印到OUTPUT.txt
但我失败了,一开始我尝试了这个过程。然后启动shell,但没有任何效果
所以我做了这个把戏,VB.NET代码用这个代码生成了一个批处理文件PROG.EXE output.txt,但是我还是失败了,虽然VB.NET创建了批处理文件并执行了,但是什么也没发生!但是当我手动运行批处理文件时,我成功了
我尝试执行批处理文件,然后发送VBCR/LF/CRLF键,但仍然没有发生任何事情
怎么了


我的VB.NET代码,我正在使用Visual Studio 2010 Professional

这是我的C代码

#include<stdio.h>  
#include<conio.h>

void main()
{
 int x;
 scanf("%d",&x);
 printf("%d",(x*x));
}
#包括
#包括
void main()
{
int x;
scanf(“%d”和&x);
printf(“%d”,x*x));
}

当我在控制台中运行prog.exe output.txt时,我的程序运行得非常好。下面是一个完全正常工作的示例。您希望按尝试的方式使用该类,但需要在进程的基础上使用该类。然后,您就可以读取流程的。下面的示例是使用VB2010编写的,但在较旧的版本中效果基本相同

Option Explicit On
Option Strict On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''//This will hold the entire output of the command that we are running
        Dim T As String

        ''//Create our process object
        Using P As New Process()
            ''//Pass it the EXE that we want to execute
            ''//NOTE: you might have to use an absolute path here
            P.StartInfo.FileName = "ping.exe"

            ''//Pass it any arguments needed
            ''//NOTE: if you pass a file name as an argument you might have to use an absolute path
            P.StartInfo.Arguments = "127.0.0.1"

            ''//Tell the process that we want to handle the commands output stream
            ''//NOTE: Some programs also write to StandardError so you might want to watch that, too
            P.StartInfo.RedirectStandardOutput = True

            ''//This is needed for the previous line to work
            P.StartInfo.UseShellExecute = False

            ''//Start the process
            P.Start()

            ''//Wrap a StreamReader around the standard output
            Using SR = P.StandardOutput
                ''//Read everything from the stream
                T = SR.ReadToEnd()
            End Using
        End Using

        ''//At this point T will hold whatever the process with the given arguments kicked out
        ''//Here we are just dumping it to the screen
        MessageBox.Show(T)
    End Sub
End Class
编辑

这是一个更新版本,它同时读取
StandardOutput
StandardError
。这次它异步读取。代码调用
选项
exe并传递无效的命令行开关,该开关将触发写入
标准错误
,而不是
标准输出
。对于您的程序,您可能应该同时监视这两者。此外,如果要将文件传递到程序中,请确保指定了文件的绝对路径,并确保如果文件路径中有空格,则将路径用引号括起来

Option Explicit On
Option Strict On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''//This will hold the entire output of the command that we are running
        Dim T As String

        ''//Create our process object
        Using P As New Process()
            ''//Pass it the EXE that we want to execute
            ''//NOTE: you might have to use an absolute path here
            P.StartInfo.FileName = "choice"

            ''//Pass it any arguments needed
            ''//NOTE: if you pass a file name as an argument you might have to use an absolute path
            ''//NOTE: I am passing an invalid parameter to show off standard error
            P.StartInfo.Arguments = "/G"

            ''//Tell the process that we want to handle the command output AND error streams
            P.StartInfo.RedirectStandardOutput = True
            P.StartInfo.RedirectStandardError = True

            ''//This is needed for the previous line to work
            P.StartInfo.UseShellExecute = False

            ''//Add handlers for both of the data received events
            AddHandler P.ErrorDataReceived, AddressOf ErrorDataReceived
            AddHandler P.OutputDataReceived, AddressOf OutputDataReceived

            ''//Start the process
            P.Start()

            ''//Start reading from both error and output
            P.BeginErrorReadLine()
            P.BeginOutputReadLine()

            ''//Signal that we want to pause until the program is done running
            P.WaitForExit()


            Me.Close()
        End Using
    End Sub

    Private Sub ErrorDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        Trace.WriteLine(String.Format("From Error  : {0}", e.Data))
    End Sub
    Private Sub OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        Trace.WriteLine(String.Format("From Output : {0}", e.Data))
    End Sub
End Class
如果文件路径中有空格,请将整个文件路径用引号括起来,这一点很重要(事实上,为了以防万一,您应该始终将其用引号括起来。)例如,这不起作用:

P.StartInfo.FileName = "attrib"
P.StartInfo.Arguments = "C:\Program Files\Windows NT\Accessories\wordpad.exe"
但这将:

P.StartInfo.FileName = "attrib"
P.StartInfo.Arguments = """C:\Program Files\Windows NT\Accessories\wordpad.exe"""
编辑2

好吧,我是个白痴。我以为你只是把一个文件名用尖括号括起来,比如
[input.txt]
,我没有意识到你使用的是真正的流重定向器!(在
input.txt
之前和之后加一个空格会有帮助。)很抱歉造成混淆

使用
进程
对象处理流重定向有两种方法。第一种方法是手动读取
input.txt
并将其写入
StandardInput
,然后读取
StandardOutput
并将其写入
output.txt
,但您不想这样做。第二种方法是使用Windows命令解释器,
cmd.exe
,它有一个特殊的参数
/C
。当传递时,它会为您执行它后面的任何字符串。所有流重定向的工作方式与您在命令行中键入它们的工作方式相同。重要的是,您传递的任何命令都要用引号括起来,以便在文件路径中看到一些双引号。所以这里有一个版本可以做到这一切:

Option Explicit On
Option Strict On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''//Full path to our various files
        Dim FullExePath As String = "C:\PROG.exe"
        Dim FullInputPath As String = "C:\input.txt"
        Dim FullOutputPath As String = "C:\output.txt"

        ''//This creates our command using quote-escaped paths, all completely wrapped in an extra set of quotes
        ''//""C:\PROG.exe" < "C:\input.txt" > "C:\output.txt""
        Dim FullCommand = String.Format("""""{0}"" < ""{1}"" > ""{2}""""", FullExePath, FullInputPath, FullOutputPath)
        ''//Create our process object
        Using P As New Process()
            ''//We are going to use the command shell and tell it to process our command for us
            P.StartInfo.FileName = "cmd"

            ''//The /C (capitalized) means "execute whatever else is passed"
            P.StartInfo.Arguments = "/C " & FullCommand

            ''//Start the process
            P.Start()

            ''//Signal to wait until the process is done running
            P.WaitForExit()
        End Using

        Me.Close()
    End Sub
End Class
下面是您传递的实际命令的外观:

cmd /C ""C:\PROG.exe" < "C:\INPUT.txt" > "C:\output.txt""
cmd/C“C:\PROG.exe”<“C:\INPUT.txt”>“C:\output.txt”
这是一个完整的代码块。我添加了错误和输出读取器,以防出现权限错误或其他问题。因此,请查看即时窗口以查看是否有任何错误被踢出。如果这不起作用,我不知道该告诉你什么

Option Explicit On
Option Strict On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''//Full path to our various files
        Dim FullExePath As String = "C:\PROG.exe"
        Dim FullInputPath As String = "C:\INPUT.txt"
        Dim FullOutputPath As String = "C:\output.txt"

        ''//This creates our command using quote-escaped paths, all completely wrapped in an extra set of quotes
        Dim FullCommand As String = """""" & FullExePath & """" & " <""" & FullInputPath & """> " & """" & FullOutputPath & """"""
        Trace.WriteLine("cmd /C " & FullCommand)


        ''//Create our process object
        Using P As New Process()
            ''//We are going to use the command shell and tell it to process our command for us
            P.StartInfo.FileName = "cmd"

            ''//Tell the process that we want to handle the command output AND error streams
            P.StartInfo.RedirectStandardError = True
            P.StartInfo.RedirectStandardOutput = True

            ''//This is needed for the previous line to work
            P.StartInfo.UseShellExecute = False

            ''//Add handlers for both of the data received events
            AddHandler P.ErrorDataReceived, AddressOf ErrorDataReceived
            AddHandler P.OutputDataReceived, AddressOf OutputDataReceived

            ''//The /C (capitalized) means "execute whatever else is passed"
            P.StartInfo.Arguments = "/C " & FullCommand

            ''//Start the process
            P.Start()


            ''//Start reading from both error and output
            P.BeginErrorReadLine()
            P.BeginOutputReadLine()


            ''//Signal to wait until the process is done running
            P.WaitForExit()
        End Using

        Me.Close()
    End Sub
    Private Sub ErrorDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        Trace.WriteLine(String.Format("From Error  : {0}", e.Data))
    End Sub
    Private Sub OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        Trace.WriteLine(String.Format("From Output : {0}", e.Data))
    End Sub
End Class
选项显式打开
选项严格限制在
公开课表格1
私有子表单1_Load(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
''//各种文件的完整路径
将FullExePath设置为String=“C:\PROG.exe”
Dim FullInputPath为String=“C:\INPUT.txt”
Dim FullOutputPath为String=“C:\output.txt”
“”//这将使用引号转义路径创建命令,所有路径都完全封装在一组额外的引号中
Dim FullCommand作为字符串=“”“&FullExePath&&&&&&FullOutputPath&”
Trace.WriteLine(“cmd/C”&FullCommand)
''//创建我们的流程对象
使用P作为新进程()
''//我们将使用命令shell并告诉它为我们处理命令
P.StartInfo.FileName=“cmd”
''//告诉进程我们要处理命令输出和错误流
P.StartInfo.RedirectStandardError=True
P.StartInfo.RedirectStandardOutput=True
''//这是前一行工作所必需的
P.StartInfo.UseShellExecute=False
''//为两个接收到的数据事件添加处理程序
AddHandler P.ErrorDataReceived,ErrorDataReceived的地址
AddHandler P.OutputDataReceived,地址OutputDataReceived
''//C(大写)表示“执行其他传递的内容”
P.StartInfo.Arguments=“/C”&FullCommand
''//开始这个过程
P.开始()
''//从错误和输出开始读取
P.BeginErrorReadLine()
P.BeginOutputReadLine()
''//发送信号等待进程运行完毕
P.WaitForExit()
终端使用
我
端接头
Private Sub ErrorDataReceived(ByVal发送方作为对象,ByVal e作为DataReceivedEventArgs)
Trace.WriteLine(String.Format(“From Error:{0}”,e.Data))
端接头
Private Sub OutputDataReceived(ByVal发送方作为对象,ByVal e作为DataReceivedEventArgs)
Trace.WriteLine(String.Format(“从输出:{0}”,e.Data))
端接头
末级

下面是一个完全有效的示例。您希望按尝试的方式使用该类,但需要在进程的基础上使用该类。然后你就可以读它了
cmd /C ""C:\PROG.exe" < "C:\INPUT.txt" > "C:\output.txt""
Option Explicit On
Option Strict On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''//Full path to our various files
        Dim FullExePath As String = "C:\PROG.exe"
        Dim FullInputPath As String = "C:\INPUT.txt"
        Dim FullOutputPath As String = "C:\output.txt"

        ''//This creates our command using quote-escaped paths, all completely wrapped in an extra set of quotes
        Dim FullCommand As String = """""" & FullExePath & """" & " <""" & FullInputPath & """> " & """" & FullOutputPath & """"""
        Trace.WriteLine("cmd /C " & FullCommand)


        ''//Create our process object
        Using P As New Process()
            ''//We are going to use the command shell and tell it to process our command for us
            P.StartInfo.FileName = "cmd"

            ''//Tell the process that we want to handle the command output AND error streams
            P.StartInfo.RedirectStandardError = True
            P.StartInfo.RedirectStandardOutput = True

            ''//This is needed for the previous line to work
            P.StartInfo.UseShellExecute = False

            ''//Add handlers for both of the data received events
            AddHandler P.ErrorDataReceived, AddressOf ErrorDataReceived
            AddHandler P.OutputDataReceived, AddressOf OutputDataReceived

            ''//The /C (capitalized) means "execute whatever else is passed"
            P.StartInfo.Arguments = "/C " & FullCommand

            ''//Start the process
            P.Start()


            ''//Start reading from both error and output
            P.BeginErrorReadLine()
            P.BeginOutputReadLine()


            ''//Signal to wait until the process is done running
            P.WaitForExit()
        End Using

        Me.Close()
    End Sub
    Private Sub ErrorDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        Trace.WriteLine(String.Format("From Error  : {0}", e.Data))
    End Sub
    Private Sub OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        Trace.WriteLine(String.Format("From Output : {0}", e.Data))
    End Sub
End Class
Public Class attributeclass
  Public index(7) As ctrarray
End Class

Public Class ctrarray
  Public nameclass As String
  Public ctrlindex(10) As ctrlindexclass
End Class

Public Class ctrlindexclass
  Public number As Integer
  Public names(10) As String
  Public status(10) As Boolean

  Sub New()
    number = 0
    For i As Integer = 0 To 10
        names(i) = "N/A"
        status(i) = False
    Next
  End Sub
End Class

Public attr As New attributeclass

Sub Main()
  attr.index(1).nameclass = "adfdsfds"
  System.Console.Write(attr.index(1).nameclass)
  System.Console.Read()
End Sub