FileStream.Seek(Int64, SeekOrigin) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Imposta la posizione corrente del flusso sul valore specificato.
public:
override long Seek(long offset, System::IO::SeekOrigin origin);
public override long Seek(long offset, System.IO.SeekOrigin origin);
override this.Seek : int64 * System.IO.SeekOrigin -> int64
Public Overrides Function Seek (offset As Long, origin As SeekOrigin) As Long
Parametri
- offset
- Int64
Punto relativo a origin da cui iniziare la ricerca.
- origin
- SeekOrigin
Specifica l'inizio, la fine o la posizione corrente come punto di riferimento per offset, usando un valore di tipo SeekOrigin.
Restituisce
Nuova posizione nel flusso.
Eccezioni
Si è verificato un errore di I/O.
Il flusso non supporta la ricerca, ad esempio se FileStream viene costruito da una pipe o da un output della console.
La ricerca viene tentata prima dell'inizio del flusso.
I metodi sono stati chiamati dopo la chiusura del flusso.
Esempio
Nell'esempio seguente viene illustrato come scrivere dati in un file, in byte byte e quindi verificare che i dati siano stati scritti correttamente.
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);
}
}
}
open System
open System.IO
let fileName = "Test#@@#.dat"
// Create random data to write to the file.
let dataArray = Array.zeroCreate 100000
Random.Shared.NextBytes dataArray
do
use fileStream = new FileStream(fileName, FileMode.Create)
// Write the data to the file, byte by byte.
for i = 0 to dataArray.Length - 1 do
fileStream.WriteByte dataArray[i]
// Set the stream position to the beginning of the file.
fileStream.Seek(0, SeekOrigin.Begin) |> ignore
// Read and verify the data.
for i in 0L .. fileStream.Length - 1L do
if dataArray[int i] <> (fileStream.ReadByte() |> byte) then
printfn "Error writing data."
exit 1
printfn $"The data was written to {fileStream.Name} and verified."
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
Nell'esempio seguente viene letto testo nella direzione inversa, dalla fine del file all'inizio del file, usando i vari SeekOrigin valori con il Seek metodo .
using System;
using System.IO;
public class FSSeek
{
public static void Main()
{
long offset;
int nextByte;
// alphabet.txt contains "abcdefghijklmnopqrstuvwxyz"
using (FileStream fs = new FileStream(@"c:\temp\alphabet.txt", FileMode.Open, FileAccess.Read))
{
for (offset = 1; offset <= fs.Length; offset++)
{
fs.Seek(-offset, SeekOrigin.End);
Console.Write((char)fs.ReadByte());
}
Console.WriteLine();
fs.Seek(20, SeekOrigin.Begin);
while ((nextByte = fs.ReadByte()) > 0)
{
Console.Write((char)nextByte);
}
Console.WriteLine();
}
}
}
// This code example displays the following output:
//
// zyxwvutsrqponmlkjihgfedcba
// uvwxyz
open System.IO
// alphabet.txt contains "abcdefghijklmnopqrstuvwxyz"
using (new FileStream(@"c:\temp\alphabet.txt", FileMode.Open, FileAccess.Read)) (fun fs ->
for offset in 1L .. fs.Length do
fs.Seek(-offset, SeekOrigin.End) |> ignore
printf $"{fs.ReadByte() |> char}"
printfn ""
fs.Seek(20, SeekOrigin.Begin) |> ignore
let mutable nextByte = fs.ReadByte()
while nextByte > 0 do
printf $"{char nextByte}"
nextByte <- fs.ReadByte())
// This code example displays the following output:
//
// zyxwvutsrqponmlkjihgfedcba
// uvwxyz
Imports System.IO
Public Class FSSeek
Public Shared Sub Main()
Dim offset As Long
Dim nextByte As Integer
' alphabet.txt contains "abcdefghijklmnopqrstuvwxyz"
Using fs As New FileStream("c:\temp\alphabet.txt", FileMode.Open, FileAccess.Read)
For offset = 1 To fs.Length
fs.Seek(-offset, SeekOrigin.End)
Console.Write(Convert.ToChar(fs.ReadByte()))
Next offset
Console.WriteLine()
fs.Seek(20, SeekOrigin.Begin)
nextByte = fs.ReadByte()
While (nextByte > 0)
Console.Write(Convert.ToChar(nextByte))
nextByte = fs.ReadByte()
End While
Console.WriteLine()
End Using
End Sub
End Class
' This code example displays the following output:
'
' zyxwvutsrqponmlkjihgfedcba
' uvwxyz
Commenti
Questo metodo esegue l'override di Stream.Seek.
Annotazioni
Utilizzare la FileStream.CanSeek proprietà per determinare se l'istanza corrente supporta la ricerca. Per altre informazioni, vedere Stream.CanSeek.
È possibile cercare qualsiasi posizione oltre la lunghezza del flusso. Quando si cerca oltre la lunghezza del file, le dimensioni del file aumentano. I dati aggiunti alla fine del file sono impostati su zero.
Per un elenco delle operazioni comuni di file e directory, vedere Attività di I/O comuni.