我使用我的.NET应用程序创建一个命名管道服务器,其名称为"TSPipe".然后我打开记事本并尝试将文件保存到"\.\ pipe\TSPipe".然后,我希望能够阅读记事本写入该管道的内容.
我仍在研究处理NamedPipeServerStream的线程的一般逻辑,但这是我的命名管道服务器的代码:
public void PipeThread() {
var sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var rule = new PipeAccessRule(sid, PipeAccessRights.ReadWrite, AccessControlType.Allow);
var sec = new PipeSecurity();
sec.AddAccessRule(rule);
while (continuePipeThread) {
pipeStream = new NamedPipeServerStream("TSPipe", PipeDirection.In, 100, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0, sec);
Program.Output.PrintLine("Waiting for connection...");
pipeStream.WaitForConnection();
Program.Output.PrintLine("Connected, reading...");
byte[] data = new byte[1024];
int lenRead = 0;
lenRead = pipeStream.Read(data, 0, data.Length);
string line = System.Text.Encoding.ASCII.GetString(data, 0, lenRead);
Program.Output.PrintLine(line);
pipeStream.Close();
pipeStream.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助,但如果有任何建议有帮助,我会通知您!