Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Eleкtro en 25 Noviembre 2012, 20:05 pm



Título: (SOLUCIONADO) Mostrar el output de la CMD
Publicado por: Eleкtro en 25 Noviembre 2012, 20:05 pm
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:
Código
  1. 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...

Código
  1.   Private Function attrib() As Boolean
  2.        Me.consolebox.Text = "Deleting the attributes of the files..."
  3.        Dim attrib_process As New Process()
  4.        Dim attrib_startinfo As New ProcessStartInfo()
  5.        Dim attrib_args As String = videofolder
  6.        attrib_startinfo.FileName = "cmd.exe "
  7.        attrib_startinfo.UseShellExecute = False
  8.        attrib_startinfo.CreateNoWindow = True
  9.        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"
  10.        attrib_startinfo.RedirectStandardOutput = True
  11.        attrib_process.EnableRaisingEvents = True
  12.        attrib_process.StartInfo = attrib_startinfo
  13.        attrib_process.Start()
  14.        Dim readerStdOut As IO.StreamReader = attrib_process.StandardOutput
  15.        Do While readerStdOut.EndOfStream = False
  16.            output = output + readerStdOut.ReadLine()
  17.        Loop
  18.        Me.consolebox.Text = "This is the result of the command:" + output
  19.    End Function

¿Y si necesito usar un comando de múltiples líneas como le hago?

por ejemplo:
Código
  1. attrib_startinfo.Arguments = "/C
  2. Echo linea 1 &
  3. (Echo linea2
  4. Echo linea 3)
  5. "


Título: Re: Mostrar el output de la CMD
Publicado por: Keyen Night en 25 Noviembre 2012, 22:07 pm
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 ;)


Título: Re: Mostrar el output de la CMD
Publicado por: Eleкtro en 25 Noviembre 2012, 22:29 pm
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":

Código
  1.    Private Function attrib() As Boolean
  2.        consolebox.AppendText(vbNewLine + "[+] Deleting the attributes of the files..." + vbNewLine + vbNewLine)
  3.        Dim attrib_process As New Process()
  4.        Dim attrib_startinfo As New ProcessStartInfo()
  5.        Dim attrib_args As String = videofolder
  6.        attrib_startinfo.FileName = "cmd.exe "
  7.        attrib_startinfo.UseShellExecute = False
  8.        attrib_startinfo.CreateNoWindow = True
  9.        attrib_startinfo.Arguments = "/C dir /B /AD C:\"
  10.        attrib_startinfo.RedirectStandardOutput = True
  11.        attrib_process.EnableRaisingEvents = True
  12.        attrib_process.StartInfo = attrib_startinfo
  13.        attrib_process.Start()
  14.        Dim readerStdOut As IO.StreamReader = attrib_process.StandardOutput
  15.        Do While readerStdOut.EndOfStream = False
  16.            consolebox.AppendText(readerStdOut.ReadLine() + vbNewLine)
  17.            consolebox.SelectionStart = consolebox.Text.Length
  18.            consolebox.ScrollToCaret()
  19.        Loop
  20.        consolebox.AppendText(vbNewLine + "[OK] attributes deleted!" + vbNewLine + vbNewLine)
  21.    End Function

Saludos