SeekOrigin 列挙体

シークするときのストリームの参照ポイントを表すフィールドを提供します。

<Serializable>
Public Enum SeekOrigin
[C#]
[Serializable]
public enum SeekOrigin
[C++]
[Serializable]
__value public enum SeekOrigin
[JScript]
public
   Serializable
enum SeekOrigin

解説

ファイルの作成およびテキストのファイルへの書き込みの例については、「 ファイルへのテキストの書き込み 」を参照してください。ファイルからのテキストの読み取りの例については、「 ファイルからのテキストの読み取り 」を参照してください。バイナリ ファイルからの読み取りおよびバイナリ ファイルへの書き込みの例については、「 新しく作成したデータ ファイルの読み取りと書き込み 」を参照してください。

SeekOrigin は、 StreamBufferedStreamFileStreamMemoryStreamBinaryWriter などのクラスの Seek メソッドで使用されます。 Seek メソッドは、 SeekOrigin で指定した位置からの相対的なオフセット パラメータを受け取ります。

メンバ

メンバ名 説明
Begin

.NET Compact Framework でもサポート。

ストリームの先頭を指定します。
Current

.NET Compact Framework でもサポート。

ストリーム内の現在位置を指定します。
End

.NET Compact Framework でもサポート。

ストリームの末尾を指定します。

使用例

[Visual Basic, C#, C++] この例では、 BaseStreamSeekSeekOrigin を使用して、基になるストリームのファイル ポインタをストリームの先頭に設定する方法を示します。

 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Dim fs As New FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Read)
        ' Create a character reader.
        Dim w As New StreamReader(fs)
        ' Set the StreamReader file pointer to the end.
        w.BaseStream.Seek(0, SeekOrigin.End)
    End Sub
End Class

[C#] 
using System;
using System.IO;

public class Test
{
    public static void Main()
    {
        FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate,
            FileAccess.Read);
        // Create a character reader.
        StreamReader w = new StreamReader(fs);
        // Set the StreamReader file pointer to the end.
        w.BaseStream.Seek(0, SeekOrigin.End);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

int main()
{
    FileStream* fs = new FileStream(S"log.txt", FileMode::OpenOrCreate,
        FileAccess::Read);
    // Create a character reader.
    StreamReader* w = new StreamReader(fs);
    // Set the StreamReader file pointer to the end.
    w->BaseStream->Seek(0, SeekOrigin::End);
}

[Visual Basic] 

' System.IO.SeekOrigin.Current


' The following example demonstrates the 'Current' member of 'SeekOrigin' enum. It creates a
' 'FileStream' object with the file access option as writer. It creates a 'StreamWriter' object 
' to write the data into the file. After writing the data successfully it reads the
' first five characeters from the file. It increments the file pointer to one character
' by using the filestream seek method. After that it displays the next five characters.
 
Imports System
Imports System.IO
Imports System.Text

Class SeekOrigin_BeginEnd
   
   'Entry point which delegates to C-style main Private Function
   Public Overloads Shared Sub Main()
      Main(System.Environment.GetCommandLineArgs())
   End Sub
   
   Overloads Public Shared Sub Main(args() As [String])
      ' Create a 'FileStream' object.
      Dim myFileStream As New FileStream("Names.txt", FileMode.OpenOrCreate, FileAccess.Write)
      ' Create a 'StreamWriter' to write the data into the file.
      Dim myStreamWriter As New StreamWriter(myFileStream)
      myStreamWriter.WriteLine("James Harry Robart")
      ' Update the 'StreamWriter'.
      myStreamWriter.Flush()
      ' Close the 'StreamWriter' and FileStream.     
      myStreamWriter.Close()
      myFileStream.Close()
      Dim myFileStream1 As New FileStream("Names.txt", FileMode.OpenOrCreate, FileAccess.Read)
      ' Set the file pointer to the begin.
      myFileStream1.Seek(0, SeekOrigin.Begin)
      Dim myByteArray(5) As [Byte]
      ' Read the first five characeters.
      myFileStream1.Read(myByteArray, 0, 5)
      Dim myEncoding As New ASCIIEncoding()
      Console.WriteLine("The Contents of the file are :")
      Console.WriteLine(myEncoding.GetString(myByteArray))
      ' Increment the file pointer from CurrentPosition by one character. 
      myFileStream1.Seek(1, SeekOrigin.Current)
      ' Read the next five characeters.
      myFileStream1.Read(myByteArray, 0, 5)
      Console.WriteLine(myEncoding.GetString(myByteArray))
      ' Close the 'FileStream'.
      myFileStream1.Close()
   End Sub 'Main 
End Class 'SeekOrigin_BeginEnd

[C#] 
// System.IO.SeekOrigin.Current

/*
    The following example demonstrates the 'Current' member of 'SeekOrigin' enum. It creates a
    'FileStream' object with the file access option as writer. It creates a 'StreamWriter' object 
    to write the data into the file. After writing the data successfully it reads the
   first five characeters from the file. It increments the file pointer to one character
   by using the filestream seek method. After that it displays the next five characters.
  */
using System;
using System.IO;
using System.Text;

   class SeekOrigin_BeginEnd
   {
      public static void Main(String[] args)
      { 

            // Create a 'FileStream' object.
            FileStream myFileStream = new FileStream("Names.txt", FileMode.OpenOrCreate, FileAccess.Write);
            // Create a 'StreamWriter' to write the data into the file.
            StreamWriter myStreamWriter = new StreamWriter(myFileStream);         
            myStreamWriter.WriteLine("James Harry Robart");
            // Update the 'StreamWriter'.
            myStreamWriter.Flush();  
            // Close the 'StreamWriter' and FileStream.     
            myStreamWriter.Close(); 
            myFileStream.Close();
            FileStream myFileStream1 = new FileStream("Names.txt", FileMode.OpenOrCreate, FileAccess.Read);
            // Set the file pointer to the begin.
            myFileStream1.Seek(0, SeekOrigin.Begin); 
            Byte[] myByteArray = new Byte[5];
            // Read the first five characeters.
            myFileStream1.Read(myByteArray,0,5);
            ASCIIEncoding myEncoding  = new ASCIIEncoding();
            Console.WriteLine("The Contents of the file are :");
            Console.WriteLine(myEncoding.GetString(myByteArray));
            // Increment the file pointer from CurrentPosition by one character. 
            myFileStream1.Seek(1,SeekOrigin.Current);
            // Read the next five characeters.
            myFileStream1.Read(myByteArray,0,5);
            Console.WriteLine(myEncoding.GetString(myByteArray));
            // Close the 'FileStream'.
            myFileStream1.Close();

      }
}

[C++] 
// System::IO::SeekOrigin::Current

/*
The following example demonstrates the 'Current' member of 'SeekOrigin' enum. It creates a
'FileStream' Object with the file access option as writer. It creates a 'StreamWriter' Object
to write the data into the file. After writing the data successfully it reads the
first five characeters from the file. It increments the file pointer to one character
by using the filestream seek method. After that it displays the next five characters.
*/
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main() { 
    // Create a 'FileStream' Object.
    FileStream* myFileStream = new FileStream(S"Names.txt", FileMode::OpenOrCreate, FileAccess::Write);
    // Create a 'StreamWriter' to write the data into the file.
    StreamWriter* myStreamWriter = new StreamWriter(myFileStream);         
    myStreamWriter->WriteLine(S"James Harry Robart");
    // Update the 'StreamWriter'.
    myStreamWriter->Flush();  
    // Close the 'StreamWriter' and FileStream.     
    myStreamWriter->Close(); 
    myFileStream->Close();
    FileStream* myFileStream1 = new FileStream(S"Names.txt", FileMode::OpenOrCreate, FileAccess::Read);
    // Set the file pointer to the begin.
    myFileStream1->Seek(0, SeekOrigin::Begin); 
    Byte myByteArray[] = new Byte[5];
    // Read the first five characeters.
    myFileStream1->Read(myByteArray,0,5);
    ASCIIEncoding* myEncoding = new ASCIIEncoding();
    Console::WriteLine(S"The Contents of the file are :");
    Console::WriteLine(myEncoding->GetString(myByteArray));
    // Increment the file pointer from CurrentPosition by one character. 
    myFileStream1->Seek(1,SeekOrigin::Current);
    // Read the next five characeters.
    myFileStream1->Read(myByteArray,0,5);
    Console::WriteLine(myEncoding->GetString(myByteArray));
    // Close the 'FileStream'.
    myFileStream1->Close();
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.IO

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み