How to truncate a file in C#?

WBOY
Release: 2023-09-23 12:21:03
forward
1432 people have browsed it

如何在 C# 中截断文件?

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

The following is the syntax -

public override void SetLength (long value);
Copy after login

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);
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template