c# - 如何将我从 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);
}
我被困在这里,所以如果可能的话,我需要有人来修复我的代码。
解决方案
经过长时间的尝试,我得到了答案
这是代码:
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
}
推荐阅读
- node.js - 将数据插入表中 - MS SQL 和 Node JS
- r - R将具有特定名称模式的文件移动到不同子目录中的文件夹
- javascript - 在 css 语句中反应原生样式表多个值
- react-native - 无论窗口大小如何,组件都只能通过滚动来访问
- python - IronPython:将 C# 列表转换为 IronPython.Runtime.List
- javascript - Google Places API 的 Fetch 请求出错
- python-3.x - 循环和爬虫项目并将元素保存为 Python 中的 json 格式
- python-3.x - 从导入的模块中终止多进程
- spring - Spring 中的注解
- javascript - ng2-smart-table 不显示