Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在最后写入 StreamWriter 流后保存文件内容。
即默认情况下,我们应该在 StreamWriter 的开头提供文件路径,但我不想这样做。
我如何在最后保存内容,即在将所有行写入流之后。
可能吗?
使用MemoryStream.
MemoryStream
using (var writer = new StreamWriter(new MemoryStream())) { writer.WriteLine("foo"); writer.Flush(); using (var file = File.Open("foo.txt")) { writer.BaseStream.Position = 0; writer.BaseStream.CopyTo(file); } }