Description: The SetLength method of the System.IO.FileStream class allows a developer to set the length of the file associated with the FileStream object. This method can be used to truncate or extend a file to a specified length.
Code Examples:
Example 1: Truncating a File FileStream fs = new FileStream("example.txt", FileMode.Open); fs.SetLength(0); fs.Close();
In this example, we open the file "example.txt" using a FileStream object. We then call the SetLength method on the file stream object to set the length of the file to 0, effectively truncating the file. Finally, we close the file stream object.
Example 2: Extending a File FileStream fs = new FileStream("example.txt", FileMode.Append); fs.SetLength(fs.Length + 1024); fs.Close();
In this example, we open the file "example.txt" using a FileStream object with FileMode.Append, which allows us to write to the end of the file. We then call the SetLength method on the file stream object to extend the length of the file to its current length plus 1024 bytes. Finally, we close the file stream object.
Package Library: This method is part of the System.IO namespace in the .NET framework, which provides for reading and writing to files and streams. It is included in the System.IO.FileSystem package library.
C# (CSharp) System.IO.FileStream.SetLength - 29 examples found. These are the top rated real world C# (CSharp) examples of System.IO.FileStream.SetLength extracted from open source projects. You can rate examples to help us improve the quality of examples.