C# 检查NamedPipeClientStream中的EOF

C# 检查NamedPipeClientStream中的EOF,c#,pipe,C#,Pipe,使用C函数,可以通过\u eof(pipeOut)检查管道的输出端是否为空,并跳过读取操作 int endOfFile = _eof(myPipeIn); if(endOfFile != 0) int aReadCount = _read(myPipeIn, aBufferPtr, 256); 是否可以对.Net的NamedPipeClientStream执行类似的操作?根据文档,在.Net管道上没有“peek”类型的功能 确定的方法是测试读取操作的结果是否为空 using (Stre

使用C函数,可以通过
\u eof(pipeOut)
检查管道的输出端是否为空,并跳过读取操作

int endOfFile = _eof(myPipeIn);
if(endOfFile != 0)
    int aReadCount = _read(myPipeIn, aBufferPtr, 256);

是否可以对.Net的NamedPipeClientStream执行类似的操作?

根据文档,在.Net管道上没有“peek”类型的功能

确定的方法是测试读取操作的结果是否为空

using (StreamReader sr = new StreamReader(pipeClient))
            {
                // Display the read text to the console
                string temp;
                while ((temp = sr.ReadLine()) != null)
                {
                    Console.WriteLine("Received from server: {0}", temp);
                }
            }

不幸的是,Bueller的提示对我不起作用,因为
ReadLine
可以阻止

但根据扎克的回答,我得出了以下结论:

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool PeekNamedPipe(SafeHandle handle,
    byte[] buffer, uint nBufferSize, ref uint bytesRead,
    ref uint bytesAvail, ref uint BytesLeftThisMessage);

static bool SomethingToRead(SafeHandle streamHandle)
{
    byte[] aPeekBuffer = new byte[1];
    uint aPeekedBytes = 0;
    uint aAvailBytes = 0;
    uint aLeftBytes = 0;

    bool aPeekedSuccess = PeekNamedPipe(
        streamHandle,
        aPeekBuffer, 1,
        ref aPeekedBytes, ref aAvailBytes, ref aLeftBytes);

    if (aPeekedSuccess && aPeekBuffer[0] != 0)
        return true;
    else
        return false;
}

在我的情况下,额外的p/Invoke调用没有问题。

谢谢您的帮助!我将尝试更改代码,使其与
StreamReader
解决方案配合使用。您是使用CreateFile还是使用CreateNamedPipe获得了SafeHandle streamHandle?