【问题标题】:On Windows, Can I use Named Pipes As Files?在 Windows 上,我可以使用命名管道作为文件吗?
【发布时间】:2012-06-12 03:21:10
【问题描述】:

我用我的 .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();
        }
    }

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

【问题讨论】:

    标签: c# io ipc named-pipes


    【解决方案1】:

    您不能从记事本写入命名管道。使用您的代码,当我尝试写入 \\\\.\pipe\TSPipe 时,记事本会自动将其更正为 UNC 路径 \\\\pipe\TSPipe。我尝试了其他几个应用程序,但它们也不起作用。除了将输入的名称传递给CreateFile之外,它们似乎还运行了一些逻辑。

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

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

    【讨论】:

    • 好的,谢谢。这回答了我的问题。我想我必须找到另一种方法来进行 IPC。
    • 它会发送该文本吗?它对我不起作用。 (Win7,提升cmd
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多