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
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.
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!