SetLength Method

FileStream.SetLength Method

Sets the length of this stream to the given value.

[Visual Basic]
Overrides Public Sub SetLength( _
   ByVal value As Long _
)
[C#]
public override void SetLength(
 long value
);
[C++]
public: void SetLength(
 __int64 value
);
[JScript]
public override function SetLength(
   value : long
);

Parameters

value
The new length of the stream.

Exceptions

Exception Type Condition
IOException An I/O error has occurred.
NotSupportedException The stream does not support both writing and seeking.
ArgumentOutOfRangeException Attempted to set the value parameter to less than 0.

Remarks

This method overrides SetLength.

If the given value is less than the current length of the stream, the stream is truncated. If the given value is larger than the current length of the stream, the stream is expanded. If the stream is expanded, the contents of the stream between the old and the new length are undefined.

A stream must support both writing and seeking for SetLength to work.

Note   Use the CanWrite property to determine whether the current instance supports writing, and the CanSeek property to determine whether seeking is supported. For additional information, see CanWrite and CanSeek.

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Rename or move a file. File.Move

FileInfo.MoveTo

Copy a file. File.Copy

FileInfo.CopyTo

Get the size of a directory. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Create a subdirectory. CreateSubdirectory
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File
See the files in a directory. Name
Sort files in a directory by size. GetFileSystemInfos

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

FileStream Class | FileStream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2016 Microsoft