111,094
社区成员




//子程序
using System;
using System.IO;
using System.IO.Pipes;
class PipeClient
{
static void Main(string[] args)
{
if (args.Length > 0)
{
using (PipeStream pipeClient = new AnonymousPipeClientStream(PipeDirection.In, args[0]))
{
Console.WriteLine("Client Current TransmissionMode: {0}.",
pipeClient.TransmissionMode);
// Anonymous Pipes do not support Message mode.
try
{
Console.WriteLine("Client Setting ReadMode to \"Byte\".");
pipeClient.ReadMode = PipeTransmissionMode.Byte;
}
catch (NotSupportedException e)
{
Console.WriteLine("EXCEPTION: {0}", e.Message);
}
using (StreamReader sr = new StreamReader(pipeClient))
{
// Display the read text to the console
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("client receive:" + temp);
}
}
}
}
Console.Write("Press Enter to continue...");
Console.ReadLine();
}
}
//主程序
using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;
class PipeServer
{
static void Main()
{
Process pipeClient = new Process();
pipeClient.StartInfo.FileName = "PipeClient.exe";
using (AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable))
{
Console.WriteLine("Server Current TransmissionMode: {0}.",
pipeServer.TransmissionMode);
// Anonymous pipes do not support Message mode.
try
{
Console.WriteLine("Server Setting ReadMode to \"Byte\".");
pipeServer.ReadMode = PipeTransmissionMode.Byte;
}
catch (NotSupportedException e)
{
Console.WriteLine("EXCEPTION: {0}", e.Message);
}
// Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments = pipeServer.GetClientHandleAsString();
pipeClient.StartInfo.UseShellExecute = false;
pipeClient.Start();
pipeServer.DisposeLocalCopyOfClientHandle();
try
{
// Read user input and send that to the client process.
using (StreamWriter sw = new StreamWriter(pipeServer))
{
sw.AutoFlush = true;
Console.WriteLine("Enter text: ");
sw.WriteLine(Console.ReadLine());
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
}
pipeClient.WaitForExit();
pipeClient.Close();
}
}