C# 仅显示来自 CMD 的特定行

C# only show one specific line from 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);