Please be advised that I am a beginner in C#.
I am writing a remote administrator tool that is sending CMD controls to the client and returning results to the server.
In this example, when I view the returned data in Debug.Console, it is in two rows as in the below image.
But what I see in the txtBox is all concatenated...
How do I split the text by rows in the txtBox as in the imported data?
This is how I populate the txtBox in the server side:
if (strdata.StartsWith("2"))
{
updateText(txtCmdConsole, strdata);
}
private void updateText(TextBox text, string strdata)
{
text.Invoke((MethodInvoker)delegate
{
text.AppendText(strdata.Substring(2));
});
}
Here is the Cmd output handler:
private void CmdOutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
StringBuilder strOutput = new StringBuilder();
if (!String.IsNullOrEmpty(outLine.Data))
{
try
{
strOutput.Append(outLine.Data);
Debug.WriteLine(strOutput);
buffer = encoder.GetBytes("2 " outLine.Data "\n");
networkStream = newclient.GetStream();
networkStream.Write(buffer, 0, buffer.Length);
networkStream.Flush();
}
catch (Exception err) { }
}
}
CodePudding user response:
If the lines are separated by \n
instead of \r\n
, simply use a string replace:
strdata = strdata.Replace("\n", "\r\n");
Then the TextBox should display the lines correctly.
Note that e.g. on Unix/Linux the defualt line separator is \n
(line feed, LF). On Windows it's \r\n
(carriage return / line feed, CR/LF).