SslStream.Seek Method
Stream.Seek
Namespace: System.Net.Security
Assembly: System.dll
Syntax
public override long Seek(
long offset,
SeekOrigin origin
)
Parameters
- offset
- Type: System.Int64
The number of bytes to move in the stream. - origin
- Type: System.IO.SeekOrigin
A System.IO.SeekOrigin enumeration that specifies the relative point of reference for the seek operation.
Return Value
Type: System.Int64
The new position within the stream.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | The stream does not support seeking. |
| ArgumentException | The value of origin is not valid. -or- The stream is in an invalid state. |
| ObjectDisposedException | The stream has been closed. |
Remarks
The Seek method allows you to change the current position of the stream. The offset parameter specifies how many bytes to move, and the origin parameter determines the starting point for the move.
For SslStream, seeking is generally not supported because the stream represents an encrypted and compressed communication channel. Operations like seeking are not meaningful in this context. If you attempt to call Seek on an SslStream, it will throw a NotSupportedException.
It is important to note that SslStream derives from Stream, which defines the Seek method. However, not all stream implementations support seeking.
Example
The following example demonstrates how an exception might be handled when attempting to seek on an SslStream.
using System;
using System.Net.Security;
using System.Net.Sockets;
using System.IO;
using System.Security.Cryptography.X509Certificates;
public class SslStreamSeekExample
{
public static void Main(string[] args)
{
// This example simulates attempting to use Seek and catching the exception.
// A real SslStream would be obtained from a connected TcpClient.
try
{
// In a real scenario, you would have an SslStream instance.
// For demonstration, we create a dummy stream that will throw.
// A typical SslStream is not seekable.
using (var dummyStream = new MemoryStream()) // MemoryStream supports seek, but SslStream does not.
{
// Wrap a non-seekable stream in SslStream or simply use a direct SslStream
// which will throw when Seek is called.
// For illustration, let's assume we have an SslStream 'sslStream'
// Example:
// TcpClient client = new TcpClient("www.example.com", 443);
// SslStream sslStream = new SslStream(client.GetStream());
// sslStream.AuthenticateAsClient("www.example.com");
// Since we can't easily create a live SslStream here for demonstration,
// we'll rely on the known behavior that it throws NotSupportedException.
Console.WriteLine("Attempting to seek on SslStream (expected to fail)...");
// Imagine 'sslStream' is a valid SslStream object
// For simulation purposes, let's throw the expected exception directly
throw new NotSupportedException("Seeking is not supported on SslStream.");
// If the above line were not there and 'sslStream' existed:
// long newPosition = sslStream.Seek(0, SeekOrigin.Begin);
// Console.WriteLine($"Seek successful. New position: {newPosition}");
}
}
catch (NotSupportedException ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Caught expected exception: {ex.Message}");
Console.ResetColor();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Caught unexpected exception: {ex.Message}");
Console.ResetColor();
}
}
}
SeekOrigin Enumeration:
- Begin: Specifies that the new position is relative to the start of the stream.
- Current: Specifies that the new position is relative to the current stream position.
- End: Specifies that the new position is relative to the end of the stream.
Requirements
| Component | Version |
|---|---|
| .NET Framework | In .NET Framework 1.1 and later versions. |
| .NET Core / .NET 5+ | Available in .NET Core and later versions. |