Hola,
Tengo una pregunta antes de la verdadera pregunta xD
¿Que control es el más adecuado para mostrar el output de la CMD? (Estoy usando un richtextbox)
Y bueno, el problema es que no consigo que el texto cambie antes d emostrar el output:
Me.consolebox.Text = "Deleting the attributes of the files..."
Todo lo demás funciona bien, pero no consigo mostrar ese string, se queda el richtextbox vacío hasta que finaliza el búcle...
Private Function attrib() As Boolean
Me.consolebox.Text = "Deleting the attributes of the files..."
Dim attrib_process As New Process()
Dim attrib_startinfo As New ProcessStartInfo()
Dim attrib_args As String = videofolder
attrib_startinfo.FileName = "cmd.exe "
attrib_startinfo.UseShellExecute = False
attrib_startinfo.CreateNoWindow = True
attrib_startinfo.Arguments = "/C PUSHD " & ControlChars.Quote & videofolder & ControlChars.Quote & " & Attrib -A -R -S -H -I /S *.* & attrib +H /S *.ico >nul & attrib +H -R /S *.ini >nul"
attrib_startinfo.RedirectStandardOutput = True
attrib_process.EnableRaisingEvents = True
attrib_process.StartInfo = attrib_startinfo
attrib_process.Start()
Dim readerStdOut As IO.StreamReader = attrib_process.StandardOutput
Do While readerStdOut.EndOfStream = False
output = output + readerStdOut.ReadLine()
Loop
Me.consolebox.Text = "This is the result of the command:" + output
End Function
¿Y si necesito usar un comando de múltiples líneas como le hago?
por ejemplo:
attrib_startinfo.Arguments = "/C
Echo linea 1 &
(Echo linea2
Echo linea 3)
"
Los bucles dejan la UI colgada, puedes usar Application.DoEvents dentro del bucle o un Me.Refresh antes de comenzar el bucle, no es la forma correcta pero para este caso esta bien. Environment.NewLine, representa el carácter de nueva linea.
Esos comandos de consola se puede llevar fácilmente a código con ayuda de IO.File, claro es solo una aclaración porque no se si necesitas a juro hacerlo en consola ;)
Gracias Keyen,
La verdad es que el comando attrib es solo es el ejemplo con el que estoy practicando para darle uso a esta función más tarde, porque lo que si necesitaré usar son utilidades de terceros como "Mediainfo.exe" y quiero mostrar el output de ese programa en cuestión.
Ya lo he solucionado usando "appendText":
Private Function attrib() As Boolean
consolebox.AppendText(vbNewLine + "[+] Deleting the attributes of the files..." + vbNewLine + vbNewLine)
Dim attrib_process As New Process()
Dim attrib_startinfo As New ProcessStartInfo()
Dim attrib_args As String = videofolder
attrib_startinfo.FileName = "cmd.exe "
attrib_startinfo.UseShellExecute = False
attrib_startinfo.CreateNoWindow = True
attrib_startinfo.Arguments = "/C dir /B /AD C:\"
attrib_startinfo.RedirectStandardOutput = True
attrib_process.EnableRaisingEvents = True
attrib_process.StartInfo = attrib_startinfo
attrib_process.Start()
Dim readerStdOut As IO.StreamReader = attrib_process.StandardOutput
Do While readerStdOut.EndOfStream = False
consolebox.AppendText(readerStdOut.ReadLine() + vbNewLine)
consolebox.SelectionStart = consolebox.Text.Length
consolebox.ScrollToCaret()
Loop
consolebox.AppendText(vbNewLine + "[OK] attributes deleted!" + vbNewLine + vbNewLine)
End Function
Saludos