C# 为什么我会收到“;无效的管道句柄”;当访问匿名管道时?

C# 为什么我会收到“;无效的管道句柄”;当访问匿名管道时?,c#,pipe,C#,Pipe,我尝试在C#中使用匿名管道,但即使使用最基本的示例也失败了。以下是服务器控制台应用程序: 命名空间服务器 { 使用制度; 使用系统诊断; 使用System.IO.Pipes; 公共静态类程序 { 公共静态void Main() { 使用(var pipe=新的匿名PipeServerStream(PipeDirection.In)) { var pipeName=pipe.getclienthandleastring(); var startInfo=new ProcessStartInfo(“

我尝试在C#中使用匿名管道,但即使使用最基本的示例也失败了。以下是服务器控制台应用程序:

命名空间服务器
{
使用制度;
使用系统诊断;
使用System.IO.Pipes;
公共静态类程序
{
公共静态void Main()
{
使用(var pipe=新的匿名PipeServerStream(PipeDirection.In))
{
var pipeName=pipe.getclienthandleastring();
var startInfo=new ProcessStartInfo(“Client.exe”,pipeName);
startInfo.UseShellExecute=false;
使用(var process=process.Start(startInfo))
{
clientHandle()的管道处理副本;
var receivedByte=pipe.ReadByte();
控制台写入线(
客户端发送了以下字节:“+receivedByte”;
process.WaitForExit();
}
}
Console.WriteLine(“按任意键继续…”);
Console.ReadKey(true);
}
}
}
以下是console客户端应用程序的源代码:

命名空间客户端
{
使用System.IO.Pipes;
公共静态类程序
{
公共静态void Main(字符串[]args)
{
使用(var pipe=new-AnonymousPipeClientStream)(
管道方向输出,参数[0]))
{
管道写入字节(0x65);
}
}
}
}
当我启动服务器时,客户端应用程序崩溃(Windows“客户端已停止工作”对话框)。服务器显示:

客户端发送了以下字节:-1

未处理的异常:System.IO.IOException:无效的管道句柄。
在[…]
在C:\[…]\Client\program.cs中的Client.program.Main(字符串[]args)处:第9行

我做错了什么?

找到了。而不是:

new AnonymousPipeServerStream(PipeDirection.In)
应该是:

new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable)