Sets the current position of this stream to the given value.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Overrides Function Seek ( _
offset As Long, _
origin As SeekOrigin _
) As Long
Dim instance As FileStream
Dim offset As Long
Dim origin As SeekOrigin
Dim returnValue As Long
returnValue = instance.Seek(offset, origin)
public override long Seek (
long offset,
SeekOrigin origin
)
public:
virtual long long Seek (
long long offset,
SeekOrigin origin
) override
public long Seek (
long offset,
SeekOrigin origin
)
public override function Seek (
offset : long,
origin : SeekOrigin
) : long
Parameters
- offset
The point relative to origin from which to begin seeking.
- origin
Specifies the beginning, the end, or the current position as a reference point for origin, using a value of type SeekOrigin.
Return Value
The new position in the stream.
This method overrides Seek.
Note |
|---|
| Use the CanSeek property to determine whether the current instance supports seeking. For additional information, see CanSeek. |
Seeking to any location beyond the length of the stream is supported. When you seek beyond the length of the file, the file size grows. In Microsoft Windows NT and greater, any data added to the end of the file is set to zero. In Microsoft Windows 98 or earlier, any data added to the end of the file is not set to zero, which means that previously deleted data is visible to the stream.
The following table lists examples of other typical or related I/O tasks.
The following code example shows how to write data to a file, byte by byte, and then verify that the data was written correctly.
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Text
Class FStream
Shared Sub Main()
Const fileName As String = "Test#@@#.dat"
' Create random data to write to the file.
Dim dataArray(100000) As Byte
Dim randomGenerator As New Random()
randomGenerator.NextBytes(dataArray)
Dim fileStream As FileStream = _
new FileStream(fileName, FileMode.Create)
Try
' Write the data to the file, byte by byte.
For i As Integer = 0 To dataArray.Length - 1
fileStream.WriteByte(dataArray(i))
Next i
' Set the stream position to the beginning of the stream.
fileStream.Seek(0, SeekOrigin.Begin)
' Read and verify the data.
For i As Integer = 0 To _
CType(fileStream.Length, Integer) - 1
If dataArray(i) <> fileStream.ReadByte() Then
Console.WriteLine("Error writing data.")
Return
End If
Next i
Console.WriteLine("The data was written to {0} " & _
"and verified.", fileStream.Name)
Finally
fileStream.Close()
End Try
End Sub
End Class
using System;
using System.IO;
class FStream
{
static void Main()
{
const string fileName = "Test#@@#.dat";
// Create random data to write to the file.
byte[] dataArray = new byte[100000];
new Random().NextBytes(dataArray);
using(FileStream
fileStream = new FileStream(fileName, FileMode.Create))
{
// Write the data to the file, byte by byte.
for(int i = 0; i < dataArray.Length; i++)
{
fileStream.WriteByte(dataArray[i]);
}
// Set the stream position to the beginning of the file.
fileStream.Seek(0, SeekOrigin.Begin);
// Read and verify the data.
for(int i = 0; i < fileStream.Length; i++)
{
if(dataArray[i] != fileStream.ReadByte())
{
Console.WriteLine("Error writing data.");
return;
}
}
Console.WriteLine("The data was written to {0} " +
"and verified.", fileStream.Name);
}
}
}
using namespace System;
using namespace System::IO;
int main()
{
String^ fileName = "Test@##@.dat";
// Create random data to write to the file.
array<Byte>^dataArray = gcnew array<Byte>(100000);
(gcnew Random)->NextBytes( dataArray );
FileStream^ fileStream = gcnew FileStream( fileName,FileMode::Create );
try
{
// Write the data to the file, byte by byte.
for ( int i = 0; i < dataArray->Length; i++ )
{
fileStream->WriteByte( dataArray[ i ] );
}
// Set the stream position to the beginning of the file.
fileStream->Seek( 0, SeekOrigin::Begin );
// Read and verify the data.
for ( int i = 0; i < fileStream->Length; i++ )
{
if ( dataArray[ i ] != fileStream->ReadByte() )
{
Console::WriteLine( "Error writing data." );
return -1;
}
}
Console::WriteLine( "The data was written to {0} "
"and verified.", fileStream->Name );
}
finally
{
fileStream->Close();
}
}
import System.*;
import System.IO.*;
class FStream
{
public static void main(String[] args)
{
final String fileName = "Test#@@#.dat";
// Create random data to write to the file.
ubyte dataArray[] = new ubyte[100000];
new Random().NextBytes(dataArray);
FileStream fileStream = new FileStream(fileName, FileMode.Create);
try {
// Write the data to the file, byte by byte.
for(int i=0;i < dataArray.length;i++) {
fileStream.WriteByte(dataArray[i]);
}
// Set the stream position to the beginning of the file.
fileStream.Seek(0, SeekOrigin.Begin);
// Read and verify the data.
for(int i=0;i < fileStream.get_Length();i++) {
if ( dataArray[i] != fileStream.ReadByte() ) {
Console.WriteLine("Error writing data.");
return;
}
}
Console.WriteLine("The data was written to {0} "
+ "and verified.", fileStream.get_Name());
}
finally {
fileStream.Dispose();
}
} //main
} //FStream
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
.NET Framework
Supported in: 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 2.0, 1.0