如何将我从 CMD 获得的结果拆分为变量?
How to Split result that I get from CMD into variable?
我正在尝试拆分来自 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);
}
我被困在这里,所以如果可能的话,我需要有人来修复我的代码。
经过多次尝试,我得到了答案
这是代码:
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
}
我正在尝试拆分来自 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);
}
我被困在这里,所以如果可能的话,我需要有人来修复我的代码。
经过多次尝试,我得到了答案
这是代码:
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
}