CMD Remote zeigt keinen Output an.

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 19 Antworten in diesem Thema. Der letzte Beitrag () ist von Fachkraftx3.

    CMD Remote zeigt keinen Output an.

    @ErfinderDesRades (hab dich mal markiert weil du das Thema ja geschrieben hast)

    Ich hab mich in folgendes Thema eingelesen => Cmd-Remote

    Da das in VB geschrieben hab ich alles in C# umgeschrieben bzw. versucht.

    Folgendes Probleme. a) BeginInvoke
    b) Output wird nicht angezeigt

    a) Da ich mir mit dem BeginInvoke nicht sicher war hab ich das Folgendermaßen "gelöst" =>

    C#-Quellcode

    1. BeginInvoke(new Action(() => this.OutputData(e.Data)));

    Es zeigt mir keinen Fehler an wollte trotzdem wissen ob das richtig is :) ?

    b) Auch wenn mein Code keine Fehler ausspuckt bekomm ich keinen Output angezeigt in meiner Richtextbox.

    Folgenden Code hab' ich:

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using Microsoft.WindowsAPICodePack;
    11. using Microsoft.WindowsAPICodePack.Shell;
    12. using System.IO;
    13. using System.Reflection;
    14. using Microsoft.WindowsAPICodePack.ApplicationServices;
    15. using Microsoft.WindowsAPICodePack.Taskbar;
    16. using System.Diagnostics;
    17. namespace DiesdasAnnanass
    18. {
    19. public partial class Form1 : Form
    20. {
    21. ThumbnailToolBarButton thumbnailButton = new ThumbnailToolBarButton(new Icon("box.ico", 16, 16), "Show MessageBox");
    22. Process _process = new Process { EnableRaisingEvents = true };
    23. public Form1()
    24. {
    25. InitializeComponent();
    26. thumbnailButton.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(handleClick);
    27. TaskbarManager.Instance.ThumbnailToolBars.AddButtons(this.Handle, thumbnailButton);
    28. Point clipPoint = new Point(0, 0);
    29. Size clipSize = new System.Drawing.Size(100, 100);
    30. Rectangle clipRectangle = new Rectangle(clipPoint, clipSize);
    31. TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(this.Handle, clipRectangle);
    32. }
    33. void Execute()
    34. {
    35. if (_process == null)
    36. {
    37. MessageBox.Show("Process nicht gestartet.");
    38. }
    39. _process.StandardInput.WriteLine(txtInput.Text);
    40. txtInput.Text = "";
    41. }
    42. private void LaunchConsoleProcess()
    43. {
    44. var _with1 = _process.StartInfo;
    45. _with1.CreateNoWindow = true;
    46. _with1.UseShellExecute = false;
    47. _with1.FileName = "cmd";
    48. _with1.RedirectStandardInput = true;
    49. _with1.RedirectStandardOutput = true;
    50. if (!_process.Start()) { MessageBox.Show("Fail"); return; }
    51. _process.BeginOutputReadLine();
    52. }
    53. delegate void OutPutDataRecievedDelegate(object sender, DataReceivedEventArgs e);
    54. private void _Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    55. {
    56. var del = new OutPutDataRecievedDelegate(_Process_OutputDataReceived);
    57. BeginInvoke(new Action(() => this.OutputData(e.Data)));
    58. }
    59. delegate void SetTextBoxTextDelegate(RichTextBox txtBox, string txt);
    60. void SetTextBoxText(RichTextBox txtBox, string txt)
    61. {
    62. if (txtBox.InvokeRequired)
    63. {
    64. var del = new SetTextBoxTextDelegate(SetTextBoxText);
    65. txtBox.Parent.Invoke(del, txtBox, txt);
    66. }
    67. else txtBox.Text += txt;
    68. }
    69. public delegate void OutPutDataDelegate();
    70. private void OutputData(string txt)
    71. {
    72. txtLog.AppendText(txt);
    73. txtLog.Select(txtLog.TextLength, 0);
    74. txtLog.ScrollToCaret();
    75. }
    76. private void Form1_Load(object sender, EventArgs e)
    77. {
    78. }
    79. void handleClick(object sender, ThumbnailButtonClickedEventArgs e)
    80. {
    81. MessageBox.Show("ThumbnailButton Example");
    82. }
    83. private void button1_Click(object sender, EventArgs e)
    84. {
    85. Execute();
    86. string output = _process.StandardOutput.ReadToEnd();
    87. txtLog.Text = output;
    88. }
    89. private void startToolStripMenuItem_Click(object sender, EventArgs e)
    90. {
    91. LaunchConsoleProcess();
    92. txtLog.Text = "";
    93. }
    94. private void txtInput_KeyDown(object sender, KeyEventArgs e)
    95. {
    96. if(e.KeyCode == Keys.Enter)
    97. {
    98. e.Handled = true;
    99. e.SuppressKeyPress = true;
    100. Execute();
    101. }
    102. }
    103. private void btExecute_Click(object sender, EventArgs e)
    104. {
    105. Execute();
    106. }
    107. }
    108. }


    Ich hab mir den mehrfach durchgelesen werd aber nicht schlau. Tipps?

    Grüße,
    Fachkraft
    EDR hat sich im VB-Code per Handles _Process.OutputDataReceived auf das Event subscribed, so einfach ist das in C# jedoch nicht.

    C#-Quellcode

    1. var processInfo = new ProcessStartInfo {
    2. CreateNoWindow = true,
    3. UseShellExecute = false,
    4. FileName = "cmd",
    5. RedirectStandardInput = true,
    6. RedirectStandardOutput = true
    7. };
    8. var process = new Process { StartInfo = processInfo, EnableRaisingEvents = true };
    9. process.OutputDataReceived += (o, e) => { //<----------------------------------------
    10. Console.WriteLine(e.Data);
    11. };
    12. //Oder natürlich für deinen Code:
    13. process.OutputDataReceived += _Process_OutputDataReceived;
    14. if (!process.Start())
    15. Console.WriteLine("Nope");
    16. process.BeginOutputReadLine();
    17. Console.Read();
    @ErfinderDesRades

    Jap wäre ich. Ich hab vorher an was anderem rumgetüfftelt deswegen dürften da noch überreste sein.
    Vielleicht könntest du mir auch erklären wenn ich bei dir: netcfg -s n eingebe, dass super funktioniert und ich output bekomme aber wenn ich das bei mir mach bekomm ich da nur den Pfad + Command ausgeworfen (Bsp: C:\Users\Logan\Documents\Visual Studio 2015\Projects\DiesdasAnnanass\DiesdasAnnanass\bin\Debug>netcfg -s n)

    Aktueller Code:

    Spoiler anzeigen

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using Microsoft.WindowsAPICodePack;
    11. using Microsoft.WindowsAPICodePack.Shell;
    12. using System.IO;
    13. using System.Reflection;
    14. using Microsoft.WindowsAPICodePack.ApplicationServices;
    15. using Microsoft.WindowsAPICodePack.Taskbar;
    16. using System.Diagnostics;
    17. namespace DiesdasAnnanass
    18. {
    19. public partial class Form1 : Form
    20. {
    21. ThumbnailToolBarButton thumbnailButton = new ThumbnailToolBarButton(new Icon("box.ico", 16, 16), "Show MessageBox");
    22. Process _process = new Process { EnableRaisingEvents = true };
    23. public Form1()
    24. {
    25. InitializeComponent();
    26. thumbnailButton.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(handleClick);
    27. TaskbarManager.Instance.ThumbnailToolBars.AddButtons(this.Handle, thumbnailButton);
    28. Point clipPoint = new Point(0, 0);
    29. Size clipSize = new System.Drawing.Size(100, 100);
    30. Rectangle clipRectangle = new Rectangle(clipPoint, clipSize);
    31. TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(this.Handle, clipRectangle);
    32. }
    33. void Execute()
    34. {
    35. if (_process == null)
    36. {
    37. MessageBox.Show("Process nicht gestartet. Evntl. erst den Process starten? :>");
    38. }
    39. _process.StandardInput.WriteLine(txtInput.Text);
    40. txtInput.Text = "";
    41. }
    42. private void LaunchConsoleProcess()
    43. {
    44. var _with1 = _process.StartInfo;
    45. _with1.CreateNoWindow = true;
    46. _with1.UseShellExecute = false;
    47. _with1.FileName = @"C:\Windows\System32\cmd.exe";
    48. _with1.RedirectStandardInput = true;
    49. _with1.RedirectStandardOutput = true;
    50. if (!_process.Start()) { MessageBox.Show("Fail"); return; }
    51. _process.BeginOutputReadLine();
    52. }
    53. delegate void OutPutDataRecievedDelegate(object sender, DataReceivedEventArgs e);
    54. private void _Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    55. {
    56. var del = new OutPutDataRecievedDelegate(_Process_OutputDataReceived);
    57. BeginInvoke(new Action(() => this.OutputData(e.Data)));
    58. }
    59. delegate void SetTextBoxTextDelegate(RichTextBox txtBox, string txt);
    60. void SetTextBoxText(RichTextBox txtBox, string txt)
    61. {
    62. if (txtBox.InvokeRequired)
    63. {
    64. var del = new SetTextBoxTextDelegate(SetTextBoxText);
    65. txtBox.Parent.Invoke(del, txtBox, txt);
    66. }
    67. else txtBox.Text += txt;
    68. }
    69. public delegate void OutPutDataDelegate();
    70. private void OutputData(string txt)
    71. {
    72. txtLog.AppendText(txt + "\n");
    73. txtLog.Select(txtLog.TextLength, 0);
    74. txtLog.ScrollToCaret();
    75. }
    76. private void Form1_Load(object sender, EventArgs e)
    77. {
    78. _process.OutputDataReceived += _Process_OutputDataReceived;
    79. }
    80. void handleClick(object sender, ThumbnailButtonClickedEventArgs e)
    81. {
    82. MessageBox.Show("ThumbnailButton Example");
    83. }
    84. private void button1_Click(object sender, EventArgs e)
    85. {
    86. Execute();
    87. }
    88. private void startToolStripMenuItem_Click(object sender, EventArgs e)
    89. {
    90. LaunchConsoleProcess();
    91. txtLog.Text = "";
    92. }
    93. private void txtInput_KeyDown(object sender, KeyEventArgs e)
    94. {
    95. if(e.KeyCode == Keys.Enter)
    96. {
    97. e.Handled = true;
    98. e.SuppressKeyPress = true;
    99. Execute();
    100. }
    101. }
    102. private void btExecute_Click(object sender, EventArgs e)
    103. {
    104. Execute();
    105. }
    106. }
    107. }
    probierma so:

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using System.IO;
    11. using System.Reflection;
    12. using System.Diagnostics;
    13. using Microsoft.WindowsAPICodePack;
    14. using Microsoft.WindowsAPICodePack.Shell;
    15. using Microsoft.WindowsAPICodePack.ApplicationServices;
    16. using Microsoft.WindowsAPICodePack.Taskbar;
    17. namespace DiesdasAnnanass {
    18. public partial class Form1 : Form {
    19. ThumbnailToolBarButton thumbnailButton = new ThumbnailToolBarButton(new Icon("box.ico", 16, 16), "Show MessageBox");
    20. Process _process = new Process { EnableRaisingEvents = true };
    21. public Form1() {
    22. InitializeComponent();
    23. thumbnailButton.Click += handleClick;
    24. TaskbarManager.Instance.ThumbnailToolBars.AddButtons(this.Handle, thumbnailButton);
    25. Point clipPoint = new Point(0, 0);
    26. Size clipSize = new System.Drawing.Size(100, 100);
    27. Rectangle clipRectangle = new Rectangle(clipPoint, clipSize);
    28. TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(this.Handle, clipRectangle);
    29. _process.OutputDataReceived += (s, e) => this.OutputData(e.Data);
    30. }
    31. void Execute() {
    32. _process.StandardInput.WriteLine(txtInput.Text);
    33. txtInput.Text = "";
    34. }
    35. private void LaunchConsoleProcess() {
    36. var _with1 = _process.StartInfo;
    37. _with1.CreateNoWindow = true;
    38. _with1.UseShellExecute = false;
    39. _with1.FileName = @"C:\Windows\System32\cmd.exe";
    40. _with1.RedirectStandardInput = true;
    41. _with1.RedirectStandardOutput = true;
    42. if (!_process.Start()) {
    43. MessageBox.Show("Fail");
    44. // hier wäre eine Exception eher angemessen - ist ja ein critical fail, wenn cmd.exe nicht zu starten ist
    45. return;
    46. }
    47. _process.BeginOutputReadLine();
    48. }
    49. private void OutputData(string txt) {
    50. txtLog.AppendText(txt + "\n");
    51. txtLog.Select(txtLog.TextLength, 0);
    52. txtLog.ScrollToCaret();
    53. }
    54. void handleClick(object sender, ThumbnailButtonClickedEventArgs e) {
    55. MessageBox.Show("ThumbnailButton Example");
    56. }
    57. private void button1_Click(object sender, EventArgs e) {
    58. Execute();
    59. }
    60. private void startToolStripMenuItem_Click(object sender, EventArgs e) {
    61. LaunchConsoleProcess();
    62. txtLog.Text = "";
    63. }
    64. private void txtInput_KeyDown(object sender, KeyEventArgs e) {
    65. if (e.KeyCode == Keys.Enter) {
    66. e.Handled = true;
    67. e.SuppressKeyPress = true;
    68. Execute();
    69. }
    70. }
    71. private void btExecute_Click(object sender, EventArgs e) {
    72. Execute();
    73. }
    74. }
    75. }
    vernünftig testen konnte ich leider nicht, weil du da so komische libs eingebunden hast.
    Beachte den Rauswurf überflüssiger Abfragen, wie _Process==null . Weil da _Process bereits inne Deklarierung initialisiert wird, kann das nicht auftreten, und dann soll man das auch nicht abfragen.
    Un Form_Load rausgeworfen - deren Kram kann ebensogut im Konstruktor stattfinden.

    Ach - und was ist eiglich der Unterschied zw button1_Click und btExecute_Click?

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „ErfinderDesRades“ ()

    C#-Quellcode

    1. private void OutputData(string txt) {
    2. if (txtLog.InvokeRequired){
    3. txtLog.Invoke(new MethodInvoker(delegate {OutputData(txt); }));
    4. return;
    5. }
    6. txtLog.AppendText(txt + "\n");
    7. txtLog.Select(txtLog.TextLength, 0);
    8. txtLog.ScrollToCaret();
    9. }

    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---
    Trotzdem bekomm ich diesen Command bei MEINEM Programm nicht angezeigt aber bei deinem schon .-.

    Quellcode

    1. ​Microsoft Windows [Version 10.0.10240]
    2. (c) 2015 Microsoft Corporation. Alle Rechte vorbehalten.
    3. C:\Users\Logan\Documents\Visual Studio 2015\Projects\DiesdasAnnanass\DiesdasAnnanass\bin\Debug>netcfg -s n
    btExecute ist ein Menüstrip
    button1 ist ein einfacher Button
    wie gesagt: ich kann nix testen, denn aufgrund der fehlenden Libs kompiliert der Kram überhaupt nicht. Da können noch soviele Standard-Controls drinne sein, ein einziges komisches, und es kompiliert nicht.

    Zum Prob: mach halt

    C#-Quellcode

    1. _process.OutputDataReceived += (s, e) => BeginInvoke(new Action<string>(OutputData), e.Data);


    @jvbsl: bitte nicht dieses komische auf InvokeRequired-Geteste (und dann noch Invoke, statt BeginInvoke).
    Da muss man nichts testen, das ist ganz sicher, dass OutputData im NebenThread aufgerufen wird.
    Weil das DataReceived-Event ja im NebenThread eintrudelt.

    Und dementsprechend kann man es auch gleich mit BeginInvoking abonnieren, wie gezeigt.

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „ErfinderDesRades“ ()

    @ErfinderDesRades

    Ich bekomm den output von ipconfig z.b schon aber nicht netcfg -s n | was bei deinem Programm super funktioniert

    Zum selber Testen (hab alles ausgeklammert was du nicht brauchst)

    C#-Quellcode

    1. ​using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using System.IO;
    11. using System.Reflection;
    12. using System.Diagnostics;
    13. /*using Microsoft.WindowsAPICodePack;
    14. using Microsoft.WindowsAPICodePack.Shell;
    15. using Microsoft.WindowsAPICodePack.ApplicationServices;
    16. using Microsoft.WindowsAPICodePack.Taskbar;*/
    17. namespace DiesdasAnnanass
    18. {
    19. public partial class Form1 : Form
    20. {
    21. //ThumbnailToolBarButton thumbnailButton = new ThumbnailToolBarButton(new Icon("box.ico", 16, 16), "Show MessageBox");
    22. Process _process = new Process { EnableRaisingEvents = true };
    23. public Form1()
    24. {
    25. InitializeComponent();
    26. /*thumbnailButton.Click += handleClick;
    27. TaskbarManager.Instance.ThumbnailToolBars.AddButtons(this.Handle, thumbnailButton);
    28. Point clipPoint = new Point(0, 0);
    29. Size clipSize = new System.Drawing.Size(100, 100);
    30. Rectangle clipRectangle = new Rectangle(clipPoint, clipSize);
    31. TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(this.Handle, clipRectangle);*/
    32. _process.OutputDataReceived += (s, e) => this.OutputData(e.Data);
    33. }
    34. void Execute()
    35. {
    36. _process.StandardInput.WriteLine(txtInput.Text);
    37. txtInput.Text = "";
    38. }
    39. private void LaunchConsoleProcess()
    40. {
    41. var _with1 = _process.StartInfo;
    42. _with1.CreateNoWindow = true;
    43. _with1.UseShellExecute = false;
    44. _with1.FileName = @"C:\Windows\System32\cmd.exe";
    45. _with1.RedirectStandardInput = true;
    46. _with1.RedirectStandardOutput = true;
    47. if (!_process.Start())
    48. {
    49. MessageBox.Show("Fail");
    50. // hier wäre eine Exception eher angemessen - ist ja ein critical fail, wenn cmd.exe nicht zu starten ist
    51. return;
    52. }
    53. _process.BeginOutputReadLine();
    54. }
    55. private void OutputData(string txt)
    56. {
    57. if (txtLog.InvokeRequired)
    58. {
    59. txtLog.Invoke(new MethodInvoker(delegate { OutputData(txt); }));
    60. return;
    61. }
    62. txtLog.AppendText(txt + "\n");
    63. txtLog.Select(txtLog.TextLength, 0);
    64. txtLog.ScrollToCaret();
    65. }
    66. /*void handleClick(object sender, ThumbnailButtonClickedEventArgs e)
    67. {
    68. MessageBox.Show("ThumbnailButton Example");
    69. }*/
    70. private void button1_Click(object sender, EventArgs e)
    71. {
    72. Execute();
    73. }
    74. private void startToolStripMenuItem_Click(object sender, EventArgs e)
    75. {
    76. LaunchConsoleProcess();
    77. txtLog.Text = "";
    78. }
    79. private void txtInput_KeyDown(object sender, KeyEventArgs e)
    80. {
    81. if (e.KeyCode == Keys.Enter)
    82. {
    83. e.Handled = true;
    84. e.SuppressKeyPress = true;
    85. Execute();
    86. }
    87. }
    88. private void btExecute_Click(object sender, EventArgs e)
    89. {
    90. Execute();
    91. }
    92. }
    93. }
    @ErfinderDesRades: Manchmal werden Funktionen mehrfach verwendet. Invoke vs. BeginInvoke lässt sich aus gründen streiten. BeginInvoke ergibt manchmal Sinn, aber Synchron ist dann oftmals doch besser/wird benötigt.
    Wenn es dir um Performance geht, da könnte man bei ganz anderen Themen ansetzen, die wesentlich mehr benötigen.
    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---