C# 逐块发送流

C# 逐块发送流,c#,process,stream,redirectstandardoutput,C#,Process,Stream,Redirectstandardoutput,对于c#中的流,我很新鲜,但我对基本知识有些熟悉 我需要帮助设置连接到未知长度流的最有效方法,并将读取的部分发送到另一个函数,直到到达流的末尾。有没有人可以看看我有什么,帮我填写while循环中的部分,或者如果while循环不是最好的方式,告诉我什么更好。非常感谢您的帮助 var processStartInfo = new ProcessStartInfo { FileName = "program.exe", RedirectStandardInput = true,

对于c#中的流,我很新鲜,但我对基本知识有些熟悉

我需要帮助设置连接到未知长度流的最有效方法,并将读取的部分发送到另一个函数,直到到达流的末尾。有没有人可以看看我有什么,帮我填写while循环中的部分,或者如果while循环不是最好的方式,告诉我什么更好。非常感谢您的帮助

var processStartInfo = new ProcessStartInfo
{
    FileName = "program.exe",
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true,
    Arguments = " -some -arguments"
};
theProcess.StartInfo = processStartInfo;
theProcess.Start();

while (!theProcess.HasExited)
{
    int count = 0;
    var b = new byte[32768]; // 32k
    while ((count = theProcess.StandardOutput.BaseStream.Read(b, 0, b.Length)) > 0)
    {
        SendChunck() // ?
    }
}

您知道通过
count
变量从原始流中读取了多少字节,因此可以将它们放入缓冲区

while ((count = theProcess.StandardOutput.BaseStream.Read(b, 0, b.Length)) > 0)
{
    byte[] actual = b.Take(count).ToArray();
    SendChunck(actual);
}
或者,如果您的
sendcunk
方法设计为将
作为参数,则可以直接将其传递给原始对象:

SendChunck(theProcess.StandardOutput.BaseStream);

然后,该方法就可以按块读取数据。

谢谢!这让我更加了解了!