C#只显示CMD中的一个特定行



我想知道cmd是否只完成了标签中显示的第10行,但我不知道这可能吗?也许用数组?该程序应在用户密码过期时获得。

private void AccountBtn_Click(object sender, EventArgs e)
{
Process pw = new Process();
pw.StartInfo.UseShellExecute = false;
pw.StartInfo.RedirectStandardOutput = true;
pw.StartInfo.FileName = "cmd.exe";
pw.StartInfo.Arguments = "/c net user " + System.Environment.UserName + " /domain";
pw.Start();
label1.Text = pw.StandardOutput.ReadToEnd();
pw.WaitForExit();
}

您可以执行以下操作:

var counter = 0;
while (!p.StandardOutput.EndOfStream)
{
var line = p.StandardOutput.ReadLine();
counter++;
if (counter == 10)
{
label1.Text = line;
break;
}
}

或者你也可以做(不太值得(:

label1.Text = p.StandardOutput
.ReadToEnd()
.Split(new[] { Environment.NewLine },
StringSplitOptions.None)
.Skip(9);

最新更新