在Windows上,我可以使用命名管道作为文件吗?

Tim*_*Tim 5 c# io ipc named-pipes

我使用我的.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)

在此先感谢您的帮助,但如果有任何建议有帮助,我会通知您!

shf*_*301 5

您无法从记事本写入命名管道。使用您的代码,当我尝试写入\\\\.\pipe\TSPipe记事本时,会自动将其更正为 UNC path \\\\pipe\TSPipe。我尝试了其他几个应用程序,但它们也不起作用。看起来他们运行了一些逻辑,而不仅仅是将输入的名称传递到CreateFile.

但是,运行类似的操作echo Hello, Word! > \\\\.\pipe\TSPipe会将文本发送Hello, World!到您的管道服务器。

所以我猜测“在 Windows 上,我可以使用命名管道作为文件吗?”这个问题的答案。有时是。

  • 它会发送该文本吗?这对我不起作用。(Win7,提升`cmd`) (3认同)