读取控制台输出以写出错误日志 VB
我正在计算机上运行一些命令,如果命令无法运行,我希望它们输出一个单独的文本文件。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几件事:每次您想要向其写入一行时,您都会创建一个新的 StreamWriter,而不是创建一个然后在需要时才写入它。您仍在使用非常基本的 shell,并且不太适合您的需要。您确实应该为此使用一个流程。
我编写了一个函数供您用来执行该进程,而不是使用 shell,该函数会将命令执行的输出返回到 ConsoleOutput 变量,然后您可以检查输出字符串。
最后,您应该使用 String.Format 而不是替换来创建要运行的命令的正确字符串。例如:
所以调整以下内容以适应,特别是 Args 变量,使用 STRING.FORMAT 函数:)
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:
So tweak the below to suit, specifically the Args variable, USING THE STRING.FORMAT function :)