简体   繁体   English

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

[英]C# only show one specific line from CMD

I want if the cmd finished only the 10th line shows in the label, but I don't know is it possible or no if yes how?Maybe with array?This program should get when the user password expire.我想 cmd 是否只完成了 label 中显示的第 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();
}

You can do the following:您可以执行以下操作:

var counter = 0;
while (!p.StandardOutput.EndOfStream)
{
    var line = p.StandardOutput.ReadLine();
    counter++;

    if (counter == 10)
    {
            label1.Text = line;
            break;
    }
}

Or you could also do (not really worth it):或者你也可以这样做(不值得):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM