C# FileStream Seek
Description
FileStream Seek
Sets the current position of this stream
to the given value.
Syntax
FileStream.Seek
has the following syntax.
public override long Seek(
long offset,
SeekOrigin origin
)
Parameters
FileStream.Seek
has the following 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 offset, using a value of type SeekOrigin.
Returns
FileStream.Seek
method returns The new position in the stream.
Example
The following example shows how to write data to a file, byte by byte, and then verify that the data was written correctly.
/*www . j a v a 2 s . c o m*/
using System;
using System.IO;
class FStream
{
static void Main()
{
const string fileName = "Test.dat";
byte[] dataArray = new byte[100];
new Random().NextBytes(dataArray);
using(FileStream fileStream = new FileStream(fileName, FileMode.Create))
{
for(int i = 0; i < dataArray.Length; i++)
{
fileStream.WriteByte(dataArray[i]);
}
fileStream.Seek(0, SeekOrigin.Begin);
for(int i = 0; i < fileStream.Length; i++)
{
Console.WriteLine(fileStream.ReadByte());
}
}
}
}
The code above generates the following result.