简体   繁体   English

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

[英]On Windows, Can I use Named Pipes As Files?

I create a named pipe server with my .NET app, and its name is "TSPipe". 我使用我的.NET应用程序创建一个命名管道服务器,其名称为“TSPipe”。 I then open Notepad and try to save the file to "\\.\\pipe\\TSPipe". 然后我打开记事本并尝试将文件保存到“\\。\\ pipe \\ TSPipe”。 I then want to be able to read what notepad wrote to that pipe. 然后,我希望能够阅读记事本写入该管道的内容。

I'm still working out the general logic for the thread that handles the NamedPipeServerStream, but here's my code for the named pipe server: 我仍在研究处理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();
        }
    }

Thanks in advance for any help, but I will let you know if any of the suggestions help! 在此先感谢您的帮助,但如果有任何建议有帮助,我会通知您!

You cannot write to a named pipe from notepad. 您无法从记事本写入命名管道。 Using your code, when I try to write to \\\\\\\\.\\pipe\\TSPipe notepad auto-corrects it to the UNC path \\\\\\\\pipe\\TSPipe . 使用您的代码,当我尝试写入\\\\\\\\.\\pipe\\TSPipe notepad时,将其自动更正为UNC路径\\\\\\\\pipe\\TSPipe I tried a couple of other applications and they did not work either. 我尝试了其他一些应用程序,但它们也没有用。 It appears that they run some logic other than just passing the name typed in into CreateFile . 它似乎运行一些逻辑,而不仅仅是将输入的名称传递给CreateFile

However running something like echo Hello, Word! > \\\\\\\\.\\pipe\\TSPipe 然而运行像echo Hello, Word! > \\\\\\\\.\\pipe\\TSPipe echo Hello, Word! > \\\\\\\\.\\pipe\\TSPipe will send the text Hello, World! echo Hello, Word! > \\\\\\\\.\\pipe\\TSPipe将发送文本Hello, World! to your pipe server. 到您的管道服务器。

So I guess the answer to the question "On Windows, Can I use Named Pipes As Files?" 所以我猜这个问题的答案是“在Windows上,我可以使用命名管道作为文件吗?” is sometimes. 有时候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM