C# Ausführen einer BATCH mit TXT Feld auls Ausgabe

  • C#

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    C# Ausführen einer BATCH mit TXT Feld auls Ausgabe

    Hallo,

    ich habe eine batch datei die in ienen Text Fenster ausgeben möchte.
    Dioe BAtch Datei führt Abruf durch und es kommen mehrere Meldungen.
    Leider zeigt er mir nur die erste Meldung an

    Ausgabe im cmd Fenster wenn ich die Datei so starte

    # Lade Datei vom bliblablub System herunter
    Starte Service ...
    Datei 'N:\Daten\test.TXT' wurde heruntergeladen
    Beendet mit Status (0): Aktion wurde erfolgreich durchgeführt.
    Drücken Sie eine beliebige Taste . . .

    kann mir jmand eine Tipp geben das ich die Anzeige in meine Text Fenster bekomme?



    C#-Quellcode

    1. private void cmdClose_Click(object sender, EventArgs e)
    2. {
    3. Close();
    4. }
    5. private void cmdStart_Click(object sender, EventArgs e)
    6. {
    7. Process cmd = new Process();
    8. cmd.OutputDataReceived += new DataReceivedEventHandler(cmd_OutputDataReceived);
    9. cmd.StartInfo.FileName = @"C:\test.cmd";
    10. cmd.StartInfo.UseShellExecute = false;
    11. cmd.StartInfo.RedirectStandardOutput = true;
    12. cmd.StartInfo.RedirectStandardInput = true;
    13. cmd.StartInfo.CreateNoWindow = true;
    14. cmd.Start();
    15. cmd.BeginOutputReadLine();
    16. }
    17. private void cmd_OutputDataReceived(object sender, DataReceivedEventArgs e)
    18. {
    19. try
    20. {
    21. if (e.Data != null && e.Data != "")
    22. {
    23. textBox.Invoke(new EventHandler(delegate {textBox.AppendText(e.Data); }));
    24. }
    25. }
    26. catch (Exception o)
    27. {
    28. MessageBox.Show(o.Message + o.Source + o.InnerException);
    29. }
    30. }

    jnpanzer schrieb:

    C#-Quellcode

    1. Process cmd = new Process();
    Pack mal diese Zeile aus der Prozedur in die Klasse.
    Ansonsten probier mal dies, Form mit 2 Button, TextBox, RichTextBox:
    Spoiler anzeigen

    C#-Quellcode

    1. using System;
    2. using System.Diagnostics;
    3. using System.Globalization;
    4. using System.Text;
    5. using System.Windows.Forms;
    6. namespace WindowsFormsApp1
    7. {
    8. public partial class Form1 : Form
    9. {
    10. private Process cmd;
    11. public Form1()
    12. {
    13. this.InitializeComponent();
    14. }
    15. private void button1_Click(object sender, EventArgs e)
    16. {
    17. this.StartProcess();
    18. }
    19. private void button2_Click(object sender, EventArgs e)
    20. {
    21. this.cmd.StandardInput.WriteLine(this.textBox1.Text);
    22. }
    23. private void StartProcess()
    24. {
    25. Encoding enc = Encoding.GetEncoding(CultureInfo.CurrentUICulture.TextInfo.OEMCodePage);
    26. this.cmd = new Process();
    27. this.cmd.StartInfo.FileName = "cmd.exe";
    28. this.cmd.StartInfo.WorkingDirectory = "";
    29. this.cmd.StartInfo.CreateNoWindow = true;
    30. this.cmd.StartInfo.UseShellExecute = false;
    31. this.cmd.StartInfo.RedirectStandardOutput = true;
    32. this.cmd.StartInfo.RedirectStandardInput = true;
    33. this.cmd.StartInfo.RedirectStandardError = true;
    34. this.cmd.StartInfo.StandardOutputEncoding = enc;
    35. this.cmd.StartInfo.StandardErrorEncoding = enc;
    36. this.cmd.OutputDataReceived += new DataReceivedEventHandler(this.WriteProcessOutput);
    37. this.cmd.Start();
    38. this.cmd.BeginOutputReadLine();
    39. }
    40. private void WriteProcessOutput(object sender, DataReceivedEventArgs e)
    41. {
    42. if (e.Data != null)
    43. {
    44. Action<string> action = new Action<string>(this.AppendText);
    45. this.BeginInvoke(action, e.Data);
    46. }
    47. }
    48. private void AppendText(string txt)
    49. {
    50. this.richTextBox1.AppendText(txt + Environment.NewLine);
    51. }
    52. private void btnClear_Click(object sender, EventArgs e)
    53. {
    54. this.richTextBox1.Clear();
    55. }
    56. }
    57. }
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    FYI: StandardOutput/OutputDataReceived beinhaltet nur die Ausgabe von stdout, nicht stderr (StandardError/ErrorDataReceived).
    Mir ist nicht klar was genau du jetzt möchtest aber eventuell hilft dir das hier?
    Process.ExitCode Property

    Wenn nicht, dann erkläre bitte genauer was du möchtest.
    @slice Ja, genau dieser ist es.
    @jnpanzer Dieser Wert, wenn != 0, bringt Dir im Fehlerfalle allerdings nicht ganz viel, weil Dir die Zuordnung des Wertes zu einem spezifischen Fehler meist nicht bekannt ist.
    docs.microsoft.com/de-de/dotne…ess.exitcode?view=net-6.0
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!