开发者

Reading Console Output to write out an error log VB

I am running some commands on computers and I would like to have them output a seperate text file if the command cannot run.

For Each strUserName As String In strLines
        Dim ReplaceCommand As String = sCommand.Replace("*", strUserName).Replace("$$$", saveFileDialog3.FileName & ".txt").Replace("###", exeSearch)
        Shell("cmd.exe /c" & ReplaceCommand, AppWinStyle.Hide, True, )


        ' If Command Cannot Execute, List Why and Move onto Next Command
            Using swrr As New StreamWriter(File.Open(ErrorLog, FileMode.OpenOrCreate))
                If Console.Readline = "blahblah" Then swrr.WriteLine("FAIL") Else swrr.WriteLine("PASS"开发者_JAVA百科)
        End Using
Next

Am I on the right track? I am getting an output to a text file but its just one line ans always says PASS.


Several things: you're creating a new StreamWriter every time you want to write a line to it, instead of creating one then just writing to it when you need to. You're still using shell which is really basic, and not really suited for what you need. You should really be using a process for this.

I've written a function for you to use to execute the process instead of using the shell, which will return the output from the command execution to the ConsoleOutput variable, which you can then check for output strings.

Lastly, you should be using String.Format instead of replace to create the correct string for the command to run. For example:

    Dim FirstName As String = "Jay"
    Dim Age As String = "twenty"
    Dim Greeting As String = String.Format("Hello {0}, I know you're {1} years old", FirstName, Age)
    ' Greetings value would be "Hello Jay, I know you're twenty years old"

So tweak the below to suit, specifically the Args variable, USING THE STRING.FORMAT function :)

 Sub DoWork()

        Dim ConsoleOutput As String = String.Empty

        Using swrr As New StreamWriter(ErrorLog, True)

            For Each strUserName As String In StrLines

                ConsoleOutput = GetCMDOuput(strUserName, saveFileDialog3.FileName, exeSearch)

                ' If Command Cannot Execute, List Why and Move onto Next Command
                If ConsoleOutput = "blahblah" Then swrr.WriteLine("FAIL") Else swrr.WriteLine("PASS")

            Next

        End Using

    End Sub

    Function GetCMDOuput(ByVal strUserName As String, ByVal strFileName As String, ByVal strExeSearch As String) As String

        Dim Args As String = String.Format("/c -paramzero {0} -paramone {1} -paramtwo {2}", strUserName, strFileName, strExeSearch)

        Dim CMD As New Process
        CMD.StartInfo.FileName = "cmd.exe"
        CMD.StartInfo.Arguments = Args
        CMD.StartInfo.UseShellExecute = False
        CMD.StartInfo.RedirectStandardInput = True
        CMD.StartInfo.RedirectStandardOutput = True
        CMD.StartInfo.CreateNoWindow = True
        CMD.Start()

        Dim retval As String = CMD.StandardOutput.ReadToEnd

        CMD.WaitForExit()

        Return retval

    End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜