Home > Mobile >  How to Split result that I get from CMD into variable?
How to Split result that I get from CMD into variable?

Time:09-21

I am trying to split result from CMD in if it is match it will insert it into my RichTextBox

this is my code:

private void NetWorkCheker_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    var proc = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            Arguments = "/c ipconfig /all",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        }
    };

    proc.Start();

    while (!proc.StandardOutput.EndOfStream)
    {
        Result.Text  = "\r\n"  proc.StandardOutput.ReadLine();
    }

    proc.WaitForExit();

    string SplitValue = Result.Text; // here I want to split the result from my RichTextBox
    MessageBox.Show(SplitValue);
}

I am stuck here so I need someone to fix my code if it is possible.

CodePudding user response:

I got the answer after long time with multiple tries

this is the code:

        private void linkLabel8_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    { 
        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                Arguments = "/c ipconfig /all",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            }
        };
        string DHCP = "";
        proc.Start();
        while (!proc.StandardOutput.EndOfStream)
        {
            if (Regex.IsMatch(proc.StandardOutput.ReadLine(), "Lease Expires . . . . . . . . . . : ")) // find results under match if match Lease Expires
            {
                DHCP  = "\r\n"   proc.StandardOutput.ReadToEnd(); // set value to var
            }

        }  
            proc.WaitForExit();
        Result.Text  = "\r\n"   DHCP.Split(new string[] {"NetBIOS"}, StringSplitOptions.RemoveEmptyEntries)[0];// split result from CMD into richtextbox
    }
  • Related