Home  >  Article  >  Backend Development  >  How to truncate a file in C#?

How to truncate a file in C#?

WBOY
WBOYforward
2023-09-23 12:21:031367browse

如何在 C# 中截断文件?

To truncate a file in C#, use the FileStream.SetLength method.

The following is the syntax -

public override void SetLength (long value);

Here, int64 = the length of the stream

Value

If the value is less than the current length of the stream: The stream was cut off. If the current position is greater than the new length, the current position is Move to the last byte of the stream.

Value > Current length

The stream is expanded and the current position remains unchanged. If the stream is expanded, The contents of the stream between the old length and the new length are undefined.

The following is an example showing the code snippet −

public void Export(string path) {
   FileStream oStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
   oStream.SetLength(Length);
}

The above is the detailed content of How to truncate a file in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete