【发布时间】:2019-02-09 21:23:32
【问题描述】:
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 上正常运行:文本“你好世界!”可以从终端的管道中读取(使用与上面完全相同的命令)就好了:
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