如何在c#中写入unix fifo

时间:2013-12-16 12:10:31

标签: c# mono

我正在尝试使用File.WriteAllText(string)和AppendAllText写入linux fifo,这是我得到的:

代码只是

void Foo()
{
    File.AppendAllText("fifo", "blah");
}

文件是“fifo”,它是使用

创建的
mkfifo fifo

blah是我试图在那里写的随机字符串。我得到了这个例外:

Unhandled Exception: System.NotSupportedException: The stream does not support seeking
  at System.IO.FileStream.Seek (Int64 offset, SeekOrigin origin) [0x00000] in <filename unknown>:0 
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0 
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
  at System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) [0x00000] in <filename unknown>:0 
  at System.IO.StreamWriter..ctor (System.String path, Boolean append) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool)
  at System.IO.File.AppendAllText (System.String path, System.String contents) [0x00000] in <filename unknown>:0 
  at listener.User.InitialiseClient (System.Object data) [0x00000] in <filename unknown>:0 
  at System.Threading.Thread.StartInternal () [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NotSupportedException: The stream does not support seeking
  at System.IO.FileStream.Seek (Int64 offset, SeekOrigin origin) [0x00000] in <filename unknown>:0 
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0 
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
  at System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) [0x00000] in <filename unknown>:0 
  at System.IO.StreamWriter..ctor (System.String path, Boolean append) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool)
  at System.IO.File.AppendAllText (System.String path, System.String contents) [0x00000] in <filename unknown>:0 
  at listener.User.InitialiseClient (System.Object data) [0x00000] in <filename unknown>:0 
  at System.Threading.Thread.StartInternal () [0x00000] in <filename unknown>:0 

该文件是使用mkfifo创建的,我可以做例如echo test >> file但是我想使用c#函数而不是调用外部二进制文件

2 个答案:

答案 0 :(得分:2)

不是使用高级函数AppendAllText,而是可以打开FileStream进行写入,然后将其包装在StreamWriter中并通过它进行书写。

我假设AppendAllText在调用FileStream之前试图寻找Write的末尾,这在fifo / Named Pipe的上下文中没有意义。

void Foo()
{
    FileStream fs = File.OpenWrite( "fifo" );

    using ( StreamWriter sw = new StreamWriter(fs) )
    {
        writer.WriteLine("Blah");
    }
}

答案 1 :(得分:1)

mkfifo 为进程间通信(IPC)创建所谓的Named Pipe

错误是您的问题明确指出 - 流不支持搜索。 Mono认为该文件是普通的流,但事实并非如此。

您应该在GitHub上查看命名空间 System.Runtime.Remoting.Channels.Ipc.Unix