Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to truncate a file in C#?
To truncate a file in C#, use the FileStream.SetLength method. This method allows you to change the size of a file by either reducing it (truncating) or expanding it to a specified length.
Syntax
Following is the syntax for the SetLength method −
public override void SetLength(long value);
Parameters
-
value − A
longrepresenting the desired length of the stream in bytes.
How It Works
The behavior of SetLength depends on whether the new value is smaller or larger than the current file size −
| Condition | Behavior |
|---|---|
| Value < Current Length | File is truncated. If current position exceeds new length, position moves to end of file. |
| Value > Current Length | File is expanded. Current position remains unchanged. New bytes are undefined (typically zeros). |
Using SetLength to Truncate a File
Example
using System;
using System.IO;
using System.Text;
class Program {
public static void Main() {
string filePath = "sample.txt";
// Create a sample file with content
File.WriteAllText(filePath, "This is a long text that will be truncated to demonstrate SetLength method.");
Console.WriteLine("Original file size: " + new FileInfo(filePath).Length + " bytes");
// Truncate the file to 20 bytes
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite)) {
stream.SetLength(20);
}
Console.WriteLine("File size after truncation: " + new FileInfo(filePath).Length + " bytes");
Console.WriteLine("File content: " + File.ReadAllText(filePath));
}
}
The output of the above code is −
Original file size: 78 bytes File size after truncation: 20 bytes File content: This is a long text
Using SetLength to Expand a File
Example
using System;
using System.IO;
class Program {
public static void Main() {
string filePath = "expand.txt";
// Create a small file
File.WriteAllText(filePath, "Hello");
Console.WriteLine("Original file size: " + new FileInfo(filePath).Length + " bytes");
// Expand the file to 20 bytes
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite)) {
stream.SetLength(20);
}
Console.WriteLine("File size after expansion: " + new FileInfo(filePath).Length + " bytes");
// Read and display the content (including null bytes)
byte[] content = File.ReadAllBytes(filePath);
Console.WriteLine("Content length: " + content.Length + " bytes");
Console.WriteLine("First 10 bytes: [" + string.Join(", ", content[..10]) + "]");
}
}
The output of the above code is −
Original file size: 5 bytes File size after expansion: 20 bytes Content length: 20 bytes First 10 bytes: [72, 101, 108, 108, 111, 0, 0, 0, 0, 0]
Complete File Truncation Example
Example
using System;
using System.IO;
class Program {
public static void TruncateFile(string path, long newSize) {
try {
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite)) {
Console.WriteLine("Original size: " + stream.Length + " bytes");
stream.SetLength(newSize);
Console.WriteLine("New size: " + stream.Length + " bytes");
}
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
public static void Main() {
string filePath = "test.txt";
// Create test file
File.WriteAllText(filePath, "This is a test file with some content for truncation demonstration.");
// Truncate to 15 bytes
TruncateFile(filePath, 15);
// Display truncated content
Console.WriteLine("Truncated content: "" + File.ReadAllText(filePath) + """);
}
}
The output of the above code is −
Original size: 68 bytes New size: 15 bytes Truncated content: "This is a test "
Conclusion
The FileStream.SetLength method provides a simple way to truncate or expand files in C#. When truncating, the file is reduced to the specified size, and when expanding, the file is padded with undefined bytes (typically zeros).
