C # FileStream object Seek () method ----- reprint


Original kevin617 Posted on 2010-12-08 11:22:00 read the number 8630 collection
launched

FileStream random read and write files can use the Seek method

 

Seek () ---------- There are two parameters of the first parameter specifies the file distances byte pointer movement. The second parameter specifies the start position of the calculated

 

a value represented SeekOrigin enumeration: SeekOrigin three values: Begin Current End.

 

aFile.Seek (8, SeekOrigin.Begin) --------- file pointer moves to eighth bytes. Start position is the first byte of the file.

 

aFile.Seek (-5, SeekOrigin.End); from the end of the file look forward five bytes. .

 

aFile.Seek (2, SeekOrigin.Current);

 

 

or less from the MSDN
FileStream.Seek method
.NET Framework 2.0
Release

    .NET Framework. 4
    .NET Framework 3.5 of
    .NET Framework 3.0
    Silverlight

current position to a given value of the stream.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

 

Syntax

public override long Seek (
    long offset,
    SeekOrigin origin
)

范例:

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);
        }
    }
}
————————————————
Disclaimer: This article is the original article CSDN bloggers "kevin617", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/kevin617/article/details/6062500

Guess you like

Origin www.cnblogs.com/bedfly/p/12130435.html