none
C# Return and Chr(13)

    Question

  • Hi,
    I am trying to convert vb.net code to C#. I have a process that basically writes ipconfig /all to a rich text box on my form. In vb.net the code is like this

    Dim pIPInfo As New Process
            pIPInfo.StartInfo.UseShellExecute = False
            pIPInfo.StartInfo.RedirectStandardOutput = True
            Try
                pIPInfo.StartInfo.FileName = "ipconfig.exe"
                pIPInfo.StartInfo.Arguments = " /all"
                pIPInfo.StartInfo.CreateNoWindow = True
                pIPInfo.Start()
                rtbIPConfig.Text = Replace(pIPInfo.StandardOutput.ReadToEnd(), Chr(13) & Chr(13), Chr(13))
                pIPInfo.WaitForExit()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
            End Try

    My conversion to C# is not working, the lines are not breaking in the rtb. Here's what I'm trying

    Process pIPInfo = new Process();
                    pIPInfo.StartInfo.UseShellExecute = false;
                    pIPInfo.StartInfo.RedirectStandardOutput = true;
                    try
                    {
                        pIPInfo.StartInfo.FileName = "ipconfig.exe";
                        pIPInfo.StartInfo.Arguments = " /all";
                        pIPInfo.StartInfo.CreateNoWindow = true;
                        pIPInfo.Start();
                        rtbIPConfig.Text = return(pIPInfo.StandardOutput.ReadToEnd(), ("\r\n") + ("\r\n"), ("\r\n"));
                        pIPInfo.WaitForExit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                    }

    I'm getting "invalid expression term return" and some errors on the ("\r\n") I tried                    
    rtbIPConfig.Text = String.Concat(pIPInfo.StandardOutput.ReadToEnd(), ("\r\n") + ("\r\n"), ("\r\n"));
    which removed all of the red squiggle lines but it didn't work on the form.

    Thanks.
    Sunday, April 12, 2009 1:25 PM

Answers

  • Thanks Guys,
    You all are always very helpful. This is the code that finally worked for me. It populates the rtb and formats correctly.

    Process pIPInfo = new Process();
                    pIPInfo.StartInfo.UseShellExecute = false;
                    pIPInfo.StartInfo.RedirectStandardOutput = true;
                    try
                    {
                        pIPInfo.StartInfo.FileName = "ipconfig.exe";
                        pIPInfo.StartInfo.Arguments = " /all";
                        pIPInfo.StartInfo.CreateNoWindow = true;
                        pIPInfo.Start();
                        rtbIPConfig.Text = pIPInfo.StandardOutput.ReadToEnd().Replace("\r" + "\r", "\r");
                        pIPInfo.WaitForExit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                    }
    • Marked as answer by Gene Magerr Sunday, April 12, 2009 6:13 PM
    Sunday, April 12, 2009 6:12 PM

All replies

  • rtbIPConfig.Text = pIpInfo.StandardOutput.ReadToEnd().Replace("\r\n"+"\r\n", .....);

    .....); Replace it with the character you want, since its not visible in your post

    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Sunday, April 12, 2009 1:34 PM
  • Yeah.  That return statement is out of place.  Must be a typo, which was supposed to be something else.

    You can call many VisualBasic type commands by adding a reference to the Microsoft.VisualBasic namespace.
    I think this is exactly the same method as used in your original VB code, but more flexible. 
    Note the optional parameters.  Leave out the right ones and it matches your original code.

    public static

    string Replace(string Expression, string Find, string Replacement, int Start = 1, int Count = -1, [Microsoft.VisualBasic.CompilerServices.OptionCompareAttribute] Microsoft.VisualBasic.CompareMethod Compare = 0)

    Member of

    Microsoft.VisualBasic.Strings

    Summary:

    Returns a string in which a specified substring has been replaced with another substring a specified number of times.

    Parameters:

    Start

    : Optional. Position within Expression where substring search is to begin. If omitted, 1 is assumed.

    Count

    : Optional. Number of substring substitutions to perform. If omitted, the default value is –1, which means "make all possible substitutions."

    Expression

    : Required. String expression containing substring to replace.

    Compare

    : Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings for values.

    Replacement

    : Required. Replacement substring.

    Find

    : Required. Substring being searched for.

    Return Values:

    If Find is zero-length or Nothing, Replace returns copy of Expression. If Replace is zero-length, Replace returns copy of Expression with no occurrences of Find. If Expression is zero-length or Nothing, or Start is greater than length of Expression, Replace returns Nothing. If Count is 0, Replace returns copy of Expression.


    Mark the best replies as answers. "Fooling computers since 1971."
    Sunday, April 12, 2009 1:39 PM
  • Well, actually I'm just trying to write to the rtb, and do a return for each line written from the process. Right now, it's showing the ipconfig /all info in the rtb, but it's not formatted, no line breaks. How would I do that? rtbIPConfig.text gets the StandardOutput from the process pIPInfo, for each line in the pIPInfo write to the rtbIPConfig and do a line break after each line. This looks like it would work but it doesn't


    rtbIPConfig.Text = (pIPInfo.StandardOutput.ReadToEnd(),("\r\n") + ("\r\n"), ("\r\n"));

    Sunday, April 12, 2009 2:37 PM
  • Have you tried adding a project reference to Microsoft.VisualBasic, and calling the VB Replace method that I posted?  There are ways to implement that function directly in .NET, but why re-invent the wheel.

    rtbIPConfig.Text = Microsoft.VisualBasic.Strings.Replace(pIPInfo.StandardOutput.ReadToEnd(), ("\r\n") + ("\r\n"), ("\r\n"));
    Mark the best replies as answers. "Fooling computers since 1971."
    Sunday, April 12, 2009 2:45 PM
  • rtbIPConfig.Text = pIpInfo.StandardOutput.ReadToEnd().Replace("\r\n"+"\r\n", "\r\n");

    Rudedog,

    Please clarify

    What is the diff between String.Replace and
    Microsoft.VisualBasic.Strings.Replace
    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Sunday, April 12, 2009 2:49 PM
  • Thanks guys,
    RudeDog2, no I didn't add a reference to Microsoft.VisualBasic but I will try that just to do it. I actually had another section of the code that worked for this as well. Here it is below.

                        string[] ret = null;
                        string s = null;
                        int r = 0;
                        while (pIPInfo.StandardOutput.EndOfStream == false)
                        {
                            s = pIPInfo.StandardOutput.ReadLine();
                            if (!string.IsNullOrEmpty(s))
                            {
                                r += 1;
                                Array.Resize(ref ret, r + 1);
                                ret[r] = (s);
                                if (!string.IsNullOrEmpty(ret[r])) rtbIPConfig.AppendText((ret[r] + ("\r\n")));
                            }
                            Application.DoEvents();
                        }
    
    Sunday, April 12, 2009 2:50 PM
  • Ok, this is indeed working but it's still not quite there. The original vb.net outputs like this

    Windows IP Configuration
    
            Host Name . . . . . . . . . . . . : genesxp
            Primary Dns Suffix  . . . . . . . : 
            Node Type . . . . . . . . . . . . : Unknown
            IP Routing Enabled. . . . . . . . : No
            WINS Proxy Enabled. . . . . . . . : No
    
    Ethernet adapter Local Area Connection:
    
            Connection-specific DNS Suffix  . : 
            Description . . . . . . . . . . . : Broadcom NetXtreme Gigabit Ethernet for hp
            Physical Address. . . . . . . . . : 00-0F-20-3B-47-4B
            Dhcp Enabled. . . . . . . . . . . : No
            IP Address. . . . . . . . . . . . : 192.168.2.113
            Subnet Mask . . . . . . . . . . . : 255.255.255.0
            Default Gateway . . . . . . . . . : 192.168.2.1
            DNS Servers . . . . . . . . . . . : 66.75.160.63
                                                66.75.160.64
    
     The C# looks like this

    Windows IP Configuration
            Host Name . . . . . . . . . . . . : genesxp
            Primary Dns Suffix  . . . . . . . : 
            Node Type . . . . . . . . . . . . : Unknown
            IP Routing Enabled. . . . . . . . : No
            WINS Proxy Enabled. . . . . . . . : No
    Ethernet adapter Local Area Connection:
            Connection-specific DNS Suffix  . : 
            Description . . . . . . . . . . . : Broadcom NetXtreme Gigabit Ethernet for hp
            Physical Address. . . . . . . . . : 00-0F-20-3B-47-4B
            Dhcp Enabled. . . . . . . . . . . : No
            IP Address. . . . . . . . . . . . : 192.168.2.113
            Subnet Mask . . . . . . . . . . . : 255.255.255.0
            Default Gateway . . . . . . . . . : 192.168.2.1
            DNS Servers . . . . . . . . . . . : 66.75.160.63
                                                66.75.160.64
    

    No line break between the Windows IP Configuration and the Ethernet adapter Local Area Connection. Now, how do i get that to happen?

    Thanks again
    Sunday, April 12, 2009 3:26 PM
  • Thanks Guys,
    You all are always very helpful. This is the code that finally worked for me. It populates the rtb and formats correctly.

    Process pIPInfo = new Process();
                    pIPInfo.StartInfo.UseShellExecute = false;
                    pIPInfo.StartInfo.RedirectStandardOutput = true;
                    try
                    {
                        pIPInfo.StartInfo.FileName = "ipconfig.exe";
                        pIPInfo.StartInfo.Arguments = " /all";
                        pIPInfo.StartInfo.CreateNoWindow = true;
                        pIPInfo.Start();
                        rtbIPConfig.Text = pIPInfo.StandardOutput.ReadToEnd().Replace("\r" + "\r", "\r");
                        pIPInfo.WaitForExit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                    }
    • Marked as answer by Gene Magerr Sunday, April 12, 2009 6:13 PM
    Sunday, April 12, 2009 6:12 PM