如何反复将命令提示符的行读取到WPF中?
我试图打开命令提示过程,输入命令,然后重复读取前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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要使用
dostbuffereddata
方法: https://learn.microsoft.com/en-us/dotnet/api/api/system.io.streamreader.discardbuffereddata?view=net-6.0I think you need to use the
DiscardBufferedData
method : https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader.discardbuffereddata?view=net-6.0e.g.: