NamedPipeServerStream无法在Windows 10上运行(适用于Windows 7)

问题描述 投票:7回答:1

似乎NamedPipeServerStream不能在Windows 10上运行。

我正在使用以下代码从我的C#应用​​程序创建命名管道。此代码已直接从MSDN示例中复制,因此应该是正确的,我想:

class Program
{
    static void Main(string[] args)
    {
        using (NamedPipeServerStream pipeServer =
                new NamedPipeServerStream("testpipe", PipeDirection.Out))
        {
            Console.WriteLine("NamedPipeServerStream object created.");
            Console.Write("Waiting for client connection... ");
            pipeServer.WaitForConnection();
            Console.WriteLine("Client connected.");
            try
            {
                using (StreamWriter sw = new StreamWriter(pipeServer))
                {
                    sw.AutoFlush = true;
                    sw.WriteLine("Hallo world!");
                    Console.WriteLine("Data was written.");
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
            }
            Console.WriteLine("Pipe closed.");
        }
    }
}

现在,如果我运行此程序,管道将成功创建。但是,在Windows 10上,每次尝试从终端中的管道读取都会立即失败,并显示错误“所有管道实例都很忙”:

Microsoft Windows [Version 10.0.17134.228]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\MuldeR>type \\.\pipe\testpipe
All pipe instances are busy.

在那之后,“主要”程序说管道坏了。

令人困惑的是,完全相同的程序在Windows 7上正常工作:文本“Hello world!”可以从终端中的管道读取(使用与上面完全相同的命令)就好了:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten.

C:\Users\testuser>type \\.\pipe\testpipe
Hallo!

我错过了什么?

谢谢!


背景:

我的目标是将字符串(密码)传递给命令行应用程序,该应用程序无法直接从命令行获取字符串。相反,命令行程序只能获取文件名,并将从指定的文件中读取字符串。但是我不想创建一个(临时的)“物理”文件,而是想通过命名管道传递字符串 - 就像我在Unix上用mkfifo做的那样。

(我无法更改命令行程序)

c# windows-10 pipe ipc
1个回答
0
投票

我们的软件与许多客户端有类似的问题,似乎某些版本的Windows 10打破了命名管道,非常令人沮丧。

MSDN文档现在声明如下:

Windows 10,版本1709:管道仅在app-container中受支持;即,从一个UWP流程到另一个UWP流程,它是同一个应用程序的一部分。此外,命名管道必须使用语法“\。\ pipe \ LOCAL \”作为管道名称。

很不清楚这意味着什么......

© www.soinside.com 2019 - 2024. All rights reserved.