我正在尝试使用 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/