首页 > 解决方案 > 如何将我从 CMD 获得的结果拆分为变量?

问题描述

我正在尝试从 CMD 中拆分结果,如果匹配,它将插入到我的 RichTextBox 中

这是我的代码:

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

我被困在这里,所以如果可能的话,我需要有人来修复我的代码。

标签: c#winforms

解决方案


经过长时间的尝试,我得到了答案

这是代码:

        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
    }

推荐阅读