Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# update imported data with \n

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

But what I see in the txtBox is all concatenated…

enter image description here

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) { }
            }
        }

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading