FileStream and StreamWriter - How to truncate the remainder of the file after writing?

前端 未结 3 388
無奈伤痛
無奈伤痛 2020-12-06 16:26
var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
using(var writer = new StreamWriter(fs))
    writer.Write(....);

If

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 16:38

    you could try writer.BaseStream.SetLength(writer.BaseStream.Position) although I'm not sure how well that would work.

    For a FileStream I think that should truncate the file to the current position.

提交回复
热议问题