2012-06-08 43 views
5

我用我的.NET应用程序创建了一个命名管道服务器,它的名字是“TSPipe”。然后我打开记事本并尝试将文件保存到“\。\ pipe \ TSPipe”。然后,我希望能够阅读记事本写入该管道的内容。在Windows上,我可以使用命名管道作为文件吗?

我还在工作了一般逻辑处理该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(); 
     } 
    } 

在此先感谢您的帮助,但我会让你知道是否有任何建议帮助!

回答

2

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

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

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

+0

好的,谢谢。这回答了我的问题。我想我必须找到另一种方式来做IPC。 – Tim

+2

它会发送文本吗?它不适合我。 (Win7,提升'cmd') –

相关问题