读取控制台输出以写出错误日志 VB

发布于 2024-11-09 13:02:44 字数 686 浏览 0 评论 0原文

我正在计算机上运行一些命令,如果命令无法运行,我希望它们输出一个单独的文本文件。

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")
        End Using
Next

我走在正确的轨道上吗?我正在将输出输出到文本文件,但它只有一行并且总是显示“通过”。

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")
        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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2024-11-16 13:02:44

有几件事:每次您想要向其写入一行时,您都会创建一个新的 StreamWriter,而不是创建一个然后在需要时才写入它。您仍在使用非常基本的 shell,并且不太适合您的需要。您确实应该为此使用一个流程。

我编写了一个函数供您用来执行该进程,而不是使用 shell,该函数会将命令执行的输出返回到 ConsoleOutput 变量,然后您可以检查输出字符串。

最后,您应该使用 String.Format 而不是替换来创建要运行的命令的正确字符串。例如:

    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"

所以调整以下内容以适应,特别是 Args 变量,使用 STRING.FORMAT 函数:)

 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文