SeekOrigin 列舉

定義

指定資料流中要用於搜尋的位置。

public enum class SeekOrigin
public enum SeekOrigin
[System.Serializable]
public enum SeekOrigin
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum SeekOrigin
type SeekOrigin = 
[<System.Serializable>]
type SeekOrigin = 
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type SeekOrigin = 
Public Enum SeekOrigin
繼承
SeekOrigin
屬性

欄位

Begin 0

指定資料流的開端。

Current 1

指定資料流中的目前位置。

End 2

指定資料流的末端。

範例

下列範例示範如何從資料流程結尾開始回溯讀取,以及如何從資料流程中的指定點讀取。

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
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

備註

SeekOrigin由 、、 FileStream BufferedStreamMemoryStreamBinaryWriter 、 和其他類別的方法 Stream 使用 Seek 。 方法 Seek 會採用相對於 所 SeekOrigin 指定位置的位移參數。

適用於

另請參閱