如何反复将命令提示符的行读取到WPF中?

发布于 2025-02-05 21:16:30 字数 1113 浏览 3 评论 0原文

我试图打开命令提示过程,输入命令,然后重复读取前5行。

一旦使用readline()函数,我就可以读取行;但是,一旦位置在第六行,我需要回到第一个。

我已经尝试了seek()函数,并且我尝试更改流的位置(均为下面的注释),但我会收到错误:

system.notsupportedException:“流不支持寻求。'

我如何回到第一行并重新阅读这5行?

我应该解决这个问题吗?

    Process process = new Process();
    
    process.StartInfo.FileName = @"cmd.exe";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.WorkingDirectory = @"/";
    process.StartInfo.CreateNoWindow = true;
    process.Start();
    
    process.StandardInput.WriteLine("someCommand");
    
    StreamReader streamReader = new StreamReader(process.StandardOutput.BaseStream);
    
    while (true)
    {
        for (int i = 0; i < 5; i++)
        {
            Debug.WriteLine(streamReader.ReadLine());
        }
    
        //streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
    
        //streamReader.BaseStream.Position = 0;
    }

I am attempting to open a command prompt process, input a command, then repeatedly read the first 5 lines.

I am able to read the lines once using the ReadLine() function; however, once the position is at the 6th line, I need to go back to the first one.

I have tried the Seek() function and I've tried changing the Position of the stream (both shown as comments below) but I get the error:

System.NotSupportedException: 'Stream does not support seeking.'

How can I go back to the first line and re-read those 5 lines?

Is there a different way that I should approach this problem?

    Process process = new Process();
    
    process.StartInfo.FileName = @"cmd.exe";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.WorkingDirectory = @"/";
    process.StartInfo.CreateNoWindow = true;
    process.Start();
    
    process.StandardInput.WriteLine("someCommand");
    
    StreamReader streamReader = new StreamReader(process.StandardOutput.BaseStream);
    
    while (true)
    {
        for (int i = 0; i < 5; i++)
        {
            Debug.WriteLine(streamReader.ReadLine());
        }
    
        //streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
    
        //streamReader.BaseStream.Position = 0;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剪不断理还乱 2025-02-12 21:16:31

我认为您需要使用dostbuffereddata方法: https://learn.microsoft.com/en-us/dotnet/api/api/system.io.streamreader.discardbuffereddata?view=net-6.0

StreamReader streamReader = new StreamReader(process.StandardOutput.BaseStream);

while (true)
{
    for (int i = 0; i < 5; i++)
    {
        Debug.WriteLine(streamReader.ReadLine());
    }

    streamReader.BaseStream.Position = 0;
    streamReader.DiscardBufferedData();
}

I think you need to use the DiscardBufferedData method : https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader.discardbuffereddata?view=net-6.0

e.g.:

StreamReader streamReader = new StreamReader(process.StandardOutput.BaseStream);

while (true)
{
    for (int i = 0; i < 5; i++)
    {
        Debug.WriteLine(streamReader.ReadLine());
    }

    streamReader.BaseStream.Position = 0;
    streamReader.DiscardBufferedData();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文