c# - 如何在 C# 中写入 unix fifo

标签 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# 函数而不是调用外部二进制文件

最佳答案

与其使用高级函数 AppendAllText,不如打开 FileStream 进行写入,然后将其包装在 StreamWriter 中并写入那个。

我假设 AppendAllText 在调用 Write 之前试图寻找到 FileStream 的末尾,这在fifo/命名管道的上下文。

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

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

关于c# - 如何在 C# 中写入 unix fifo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20610660/

相关文章:

c# - 在 UWP App 中将蒙版应用于位图图像

c# - 在 C# 控制台窗口中打印

c# - 列出使用 Mono.Cecil 调用方法的所有引用

asp.net - Razor View Engine 是否适用于 Mono?

c# - 如何在单声道的 C# 中编写简单的基于 REST 的服务器?

c# - 匿名对象的 Entity Framework ObjectMaterialized

C# - 使用 HTML Agility Pack 获取标签内的文本

c# - LINQ:从另一个 IEnumerable<T> 创建 IEnumerable<T>?

c# - 在 Mono 和 Ubuntu 14.04 上使用 ASP.NET 5 时出现 NullReferenceException 崩溃

c# - 使用 GTK 构建 GUI#