C#常用文件操作总结

在C#中,常用的文件操作包含文件的移动,复制,文件的读写,其中文件的读写分为文本文件以及二进制文件。

文本文件与二进制文件有各自的优缺点,文本文件按照字符的ASCII码进行存储,所以你可以直接用记事本打开文本文件,可以看到文件的内容,但是文件的所有的内容在内存中的表示和在记事本中的表示是不一样的,所以读入内存会存在转码的时间消耗,如果你想让文件直观的话,可以采用文本文件。

二进制文件则是直接存储你需要存储内容的在内存中的直接表示,在读取的时候比较快捷,而且二进制文件的大小要比文本文件小很多。


常用的文件操作类是File,File类是一个静态类,所有的成员函数都为静态函数,所以File类的效率比其他的例如FileInfo类的效率要高。一般情况下,File和FileInfo可以实现的功能大同小异。可以根据自己的需要选择不同的类。


File支持文件的基本操作,包括提供了创建、复制、删除、移动、打开以及获取文件的其他的相关属性的一些方法。

同时,为了更好的支持File文件的操作,C#中定义了一个文件流FileStream,可以将文件加载到文件流FileStream中进行更加方便的操作。

为了对FileStream进行操作,C#定义了两组最常用的操作FileStream的类,一组是StreamReader, StreamWriter, 另外一组是BinaryReader, BinaryWriter,。这两组类分别提供了进行文本文件读写以及二进制文件的读取操作。


文件的操作还有一些其他的类,但是对于常用的一些基本的文件操作,有File, FileStream, StreamReader, StreamWriter,BinaryReader, BinaryWriter,这几个类就足够了。


File类的定义,如下所示:


File MembersVisual Studio 2010

Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.

The File type exposes the following members.

Methods

 NameDescription
Public methodStatic memberAppendAllLines(String, IEnumerable<String>) Appends lines to a file, and then closes the file.
Public methodStatic memberAppendAllLines(String, IEnumerable<String>, Encoding) Appends lines to a file by using a specified encoding, and then closes the file.
Public methodStatic memberAppendAllText(String, String)Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
Public methodStatic memberAppendAllText(String, String, Encoding)Appends the specified string to the file, creating the file if it does not already exist.
Public methodStatic memberSupported by the XNA FrameworkAppendTextCreates a StreamWriter that appends UTF-8 encoded text to an existing file.
Public methodStatic memberSupported by the XNA FrameworkCopy(String, String)Copies an existing file to a new file. Overwriting a file of the same name is not allowed.
Public methodStatic memberSupported by the XNA FrameworkCopy(String, String, Boolean)Copies an existing file to a new file. Overwriting a file of the same name is allowed.
Public methodStatic memberSupported by the XNA FrameworkCreate(String)Creates or overwrites a file in the specified path.
Public methodStatic memberSupported by the XNA FrameworkCreate(String, Int32)Creates or overwrites the specified file.
Public methodStatic memberCreate(String, Int32, FileOptions)Creates or overwrites the specified file, specifying a buffer size and a FileOptions value that describes how to create or overwrite the file.
Public methodStatic memberCreate(String, Int32, FileOptions, FileSecurity)Creates or overwrites the specified file with the specified buffer size, file options, and file security.
Public methodStatic memberSupported by the XNA FrameworkCreateTextCreates or opens a file for writing UTF-8 encoded text.
Public methodStatic memberDecryptDecrypts a file that was encrypted by the current account using the Encrypt method.
Public methodStatic memberSupported by the XNA FrameworkDeleteDeletes the specified file. An exception is not thrown if the specified file does not exist.
Public methodStatic memberEncryptEncrypts a file so that only the account used to encrypt the file can decrypt it.
Public methodStatic memberSupported by the XNA FrameworkExistsDetermines whether the specified file exists.
Public methodStatic memberGetAccessControl(String)Gets a FileSecurity object that encapsulates the access control list (ACL) entries for a specified file.
Public methodStatic memberGetAccessControl(String, AccessControlSections)Gets a FileSecurity object that encapsulates the specified type of access control list (ACL) entries for a particular file.
Public methodStatic memberGetAttributesGets the FileAttributes of the file on the path.
Public methodStatic memberSupported by the XNA FrameworkGetCreationTimeReturns the creation date and time of the specified file or directory.
Public methodStatic memberGetCreationTimeUtcReturns the creation date and time, in coordinated universal time (UTC), of the specified file or directory.
Public methodStatic memberSupported by the XNA FrameworkGetLastAccessTimeReturns the date and time the specified file or directory was last accessed.
Public methodStatic memberGetLastAccessTimeUtcReturns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.
Public methodStatic memberSupported by the XNA FrameworkGetLastWriteTimeReturns the date and time the specified file or directory was last written to.
Public methodStatic memberGetLastWriteTimeUtcReturns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.
Public methodStatic memberSupported by the XNA FrameworkMoveMoves a specified file to a new location, providing the option to specify a new file name.
Public methodStatic memberSupported by the XNA FrameworkOpen(String, FileMode)Opens a FileStream on the specified path with read/write access.
Public methodStatic memberSupported by the XNA FrameworkOpen(String, FileMode, FileAccess)Opens a FileStream on the specified path, with the specified mode and access.
Public methodStatic memberSupported by the XNA FrameworkOpen(String, FileMode, FileAccess, FileShare)Opens a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.
Public methodStatic memberSupported by the XNA FrameworkOpenReadOpens an existing file for reading.
Public methodStatic memberSupported by the XNA FrameworkOpenTextOpens an existing UTF-8 encoded text file for reading.
Public methodStatic memberSupported by the XNA FrameworkOpenWriteOpens an existing file or creates a new file for writing.
Public methodStatic memberReadAllBytesOpens a binary file, reads the contents of the file into a byte array, and then closes the file.
Public methodStatic memberReadAllLines(String)Opens a text file, reads all lines of the file, and then closes the file.
Public methodStatic memberReadAllLines(String, Encoding)Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
Public methodStatic memberReadAllText(String)Opens a text file, reads all lines of the file, and then closes the file.
Public methodStatic memberReadAllText(String, Encoding)Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
Public methodStatic memberReadLines(String)Reads the lines of a file.
Public methodStatic memberReadLines(String, Encoding)Read the lines of a file that has a specified encoding.
Public methodStatic memberReplace(String, String, String)Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file.
Public methodStatic memberReplace(String, String, String, Boolean)Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file and optionally ignores merge errors.
Public methodStatic memberSetAccessControlApplies access control list (ACL) entries described by a FileSecurity object to the specified file.
Public methodStatic memberSetAttributesSets the specified FileAttributes of the file on the specified path.
Public methodStatic memberSetCreationTimeSets the date and time the file was created.
Public methodStatic memberSetCreationTimeUtcSets the date and time, in coordinated universal time (UTC), that the file was created.
Public methodStatic memberSetLastAccessTimeSets the date and time the specified file was last accessed.
Public methodStatic memberSetLastAccessTimeUtcSets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.
Public methodStatic memberSetLastWriteTimeSets the date and time that the specified file was last written to.
Public methodStatic memberSetLastWriteTimeUtcSets the date and time, in coordinated universal time (UTC), that the specified file was last written to.
Public methodStatic memberWriteAllBytesCreates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
Public methodStatic memberWriteAllLines(String, IEnumerable<String>) Creates a new file, writes a collection of strings to the file, and then closes the file.
Public methodStatic memberWriteAllLines(String,String[]) Creates a new file, write the specified string array to the file, and then closes the file.
Public methodStatic memberWriteAllLines(String, IEnumerable<String>, Encoding) Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.
Public methodStatic memberWriteAllLines(String,String[], Encoding) Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.
Public methodStatic memberWriteAllText(String, String)Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
Public methodStatic memberWriteAllText(String, String, Encoding)Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten.
Top

然后在看看我们的FileStream类的成员:


FileStream MembersVisual Studio 2010

Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.

The FileStream type exposes the following members.

Constructors

 NameDescription
Public methodFileStream(IntPtr, FileAccess)Obsolete. Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission.
Public methodFileStream(SafeFileHandle, FileAccess)Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission.
Public methodSupported by the XNA FrameworkFileStream(String, FileMode)Initializes a new instance of the FileStream class with the specified path and creation mode.
Public methodFileStream(IntPtr, FileAccess, Boolean)Obsolete. Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission and FileStream instance ownership.
Public methodFileStream(SafeFileHandle, FileAccess, Int32)Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, and buffer size.
Public methodSupported by the XNA FrameworkFileStream(String, FileMode, FileAccess)Initializes a new instance of the FileStream class with the specified path, creation mode, and read/write permission.
Public methodFileStream(IntPtr, FileAccess, Boolean, Int32)Obsolete. Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, FileStream instance ownership, and buffer size.
Public methodFileStream(SafeFileHandle, FileAccess, Int32, Boolean)Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, buffer size, and synchronous or asynchronous state.
Public methodSupported by the XNA FrameworkFileStream(String, FileMode, FileAccess, FileShare)Initializes a new instance of the FileStream class with the specified path, creation mode, read/write permission, and sharing permission.
Public methodFileStream(IntPtr, FileAccess, Boolean, Int32, Boolean)Obsolete. Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, FileStream instance ownership, buffer size, and synchronous or asynchronous state.
Public methodSupported by the XNA FrameworkFileStream(String, FileMode, FileAccess, FileShare, Int32)Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, and buffer size.
Public methodSupported by the XNA FrameworkFileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, buffer size, and synchronous or asynchronous state.
Public methodFileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, and additional file options.
Public methodFileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions)Initializes a new instance of the FileStream class with the specified path, creation mode, access rights and sharing permission, the buffer size, and additional file options.
Public methodFileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)Initializes a new instance of the FileStream class with the specified path, creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security.
Top
Methods

 NameDescription
Public methodSupported by the XNA FrameworkBeginReadBegins an asynchronous read. (Overrides Stream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) .)

In XNA Framework 3.0, this member is inherited from Stream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) .
Public methodSupported by the XNA FrameworkBeginReadBegins an asynchronous read operation. (Inherited from Stream.)
Public methodSupported by the XNA FrameworkBeginWriteBegins an asynchronous write. (Overrides Stream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) .)

In XNA Framework 3.0, this member is inherited from Stream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) .
Public methodSupported by the XNA FrameworkBeginWriteBegins an asynchronous write operation. (Inherited from Stream.)
Public methodSupported by the XNA FrameworkCloseCloses the current stream and releases any resources (such as sockets and file handles) associated with the current stream. (Inherited from Stream.)
Public methodCopyTo(Stream)Reads all the bytes from the current stream and writes them to the destination stream. (Inherited from Stream.)
Public methodCopyTo(Stream, Int32)Reads all the bytes from the current stream and writes them to a destination stream, using a specified buffer size. (Inherited from Stream.)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Protected methodSupported by the XNA FrameworkCreateWaitHandleObsolete. Allocates a WaitHandle object. (Inherited from Stream.)
Public methodSupported by the XNA FrameworkDispose()Infrastructure. Releases all resources used by the Stream. (Inherited from Stream.)
Protected methodSupported by the XNA FrameworkDispose(Boolean)Releases the unmanaged resources used by the FileStream and optionally releases the managed resources. (Overrides Stream.Dispose(Boolean) .)
Public methodSupported by the XNA FrameworkEndReadWaits for the pending asynchronous read to complete. (Overrides Stream.EndRead(IAsyncResult) .)

In XNA Framework 3.0, this member is inherited from Stream.EndRead(IAsyncResult).
Public methodSupported by the XNA FrameworkEndReadWaits for the pending asynchronous read to complete. (Inherited from Stream.)
Public methodSupported by the XNA FrameworkEndWriteEnds an asynchronous write, blocking until the I/O operation has completed. (Overrides Stream.EndWrite(IAsyncResult) .)

In XNA Framework 3.0, this member is inherited from Stream.EndWrite(IAsyncResult).
Public methodSupported by the XNA FrameworkEndWriteEnds an asynchronous write operation. (Inherited from Stream.)
Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkFinalizeEnsures that resources are freed and other cleanup operations are performed when the garbage collector reclaims the FileStream. (Overrides Object.Finalize().)
Public methodSupported by the XNA FrameworkFlush()Clears buffers for this stream and causes any buffered data to be written to the file. (Overrides Stream.Flush().)
Public methodFlush(Boolean)Clears buffers for this stream and causes any buffered data to be written to the file, and also clears all intermediate file buffers.
Public methodGetAccessControlGets a FileSecurity object that encapsulates the access control list (ACL) entries for the file described by the current FileStream object.
Public methodSupported by the XNA FrameworkGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodLockPrevents other processes from reading from or writing to the FileStream.
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Protected methodObjectInvariantInfrastructure. Provides support for a Contract. (Inherited from Stream.)
Public methodSupported by the XNA FrameworkReadReads a block of bytes from the stream and writes the data in a given buffer. (Overrides Stream.Read(Byte[], Int32, Int32) .)
Public methodSupported by the XNA FrameworkReadByteReads a byte from the file and advances the read position one byte. (Overrides Stream.ReadByte().)
Public methodSupported by the XNA FrameworkSeekSets the current position of this stream to the given value. (Overrides Stream.Seek(Int64, SeekOrigin) .)
Public methodSetAccessControlApplies access control list (ACL) entries described by a FileSecurity object to the file described by the current FileStream object.
Public methodSupported by the XNA FrameworkSetLengthSets the length of this stream to the given value. (Overrides Stream.SetLength(Int64) .)
Public methodSupported by the XNA FrameworkToStringReturns a String that represents the current Object. (Inherited from Object.)
Public methodUnlockAllows access by other processes to all or part of a file that was previously locked.
Public methodSupported by the XNA FrameworkWriteWrites a block of bytes to this stream using data from a buffer. (Overrides Stream.Write(Byte[], Int32, Int32) .)
Public methodSupported by the XNA FrameworkWriteByteWrites a byte to the current position in the file stream. (Overrides Stream.WriteByte(Byte) .)
Top
Properties

 NameDescription
Public propertySupported by the XNA FrameworkCanReadGets a value indicating whether the current stream supports reading. (Overrides Stream.CanRead .)
Public propertySupported by the XNA FrameworkCanSeekGets a value indicating whether the current stream supports seeking. (Overrides Stream.CanSeek .)
Public propertySupported by the XNA FrameworkCanTimeoutGets a value that determines whether the current stream can time out. (Inherited from Stream.)
Public propertySupported by the XNA FrameworkCanWriteGets a value indicating whether the current stream supports writing. (Overrides Stream.CanWrite .)
Public propertyHandleObsolete. Gets the operating system file handle for the file that the current FileStream object encapsulates.
Public propertySupported by the XNA FrameworkIsAsyncGets a value indicating whether the FileStream was opened asynchronously or synchronously.
Public propertySupported by the XNA FrameworkLengthGets the length in bytes of the stream. (Overrides Stream.Length .)
Public propertySupported by the XNA FrameworkNameGets the name of the FileStream that was passed to the constructor.
Public propertySupported by the XNA FrameworkPositionGets or sets the current position of this stream. (Overrides Stream.Position .)
Public propertySupported by the XNA FrameworkReadTimeoutGets or sets a value, in miliseconds, that determines how long the stream will attempt to read before timing out. (Inherited from Stream.)
Public propertySafeFileHandleGets a SafeFileHandle object that represents the operating system file handle for the file that the current FileStream object encapsulates.
Public propertySupported by the XNA FrameworkWriteTimeoutGets or sets a value, in miliseconds, that determines how long the stream will attempt to write before timing out. (Inherited from Stream.)
Top


可以看到都是对字节流的操作,这种对于字节流的操作,对于大部分来说还是比较困难的,所以c#又帮助我们封装了对FileStream的操作,那就是StreamReader, StreamWriter, BinaryReader, BinaryWriter.

下面分别看看这两组类怎么使用:

StreamReader, StreamWriter


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileOperater
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            if (File.Exists("record.txt"))
            {
                FileStream fs = new FileStream("record.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                while (!sr.EndOfStream)
                {
                    string msg = sr.ReadLine();
                    this.richTextBox1.Text += msg + Environment.NewLine;
                }
                sr.Close();
                fs.Close();
            }
 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("record.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            string text = richTextBox1.Text;
            sw.WriteLine(text);
            // 添加一些其他的信息
            sw.WriteLine("i love C# programming!");
            sw.WriteLine("what's ur favourate programming language?");
            sw.Flush();
            sw.Close();
            fs.Close();
        }
    }
}

这个时候打开文本文件,可以看到你输入的内容和添加的内容。


BinaryWriter, BinaryReader.

这个时候我们添加三个TextBox ,可以将TextBox中输入的数字转换为对应的整数,然后将整数以及在richTextBox中的字符串都以二进制的方式写入文件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileOperater
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            if (File.Exists("binary"))
            {
                FileStream fs = new FileStream("binary", FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                int a1 = br.ReadInt32();
                int a2 = br.ReadInt32();
                int a3 = br.ReadInt32();
                string msg = br.ReadString();
                this.textBox1.Text = a1.ToString();
                textBox2.Text = a2.ToString();
                textBox3.Text = a3.ToString();
                richTextBox1.Text = msg;

                br.Close();
                fs.Close();
            }
 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("record.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            string text = richTextBox1.Text;
            sw.WriteLine(text);
            // 添加一些其他的信息
            sw.WriteLine("i love C# programming!");
            sw.WriteLine("what's ur favourate programming language?");
            sw.Flush();
            sw.Close();
            fs.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int a1 = int.Parse(textBox1.Text.Trim());
            int a2 = int.Parse(textBox2.Text.Trim());
            int a3 = int.Parse(textBox3.Text.Trim());

            string msg = richTextBox1.Text.Trim();
            FileStream fs = new FileStream("binary", FileMode.OpenOrCreate);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(a1);
            bw.Write(a2);
            bw.Write(a3);
            bw.Write(msg);
            bw.Flush();
            bw.Close();
            fs.Close();
        }
    }
}

操作二进制文件,一定要按照写入文件的顺序来读文件,否则肯定会出现不可预知的错误。



StreamReader MembersVisual Studio 2010

Implements a TextReader that reads characters from a byte stream in a particular encoding.

The StreamReader type exposes the following members.

Constructors

 NameDescription
Public methodSupported by the XNA FrameworkStreamReader(Stream)Initializes a new instance of the StreamReader class for the specified stream.
Public methodSupported by the XNA FrameworkStreamReader(String)Initializes a new instance of the StreamReader class for the specified file name.
Public methodSupported by the XNA FrameworkStreamReader(Stream, Boolean)Initializes a new instance of the StreamReader class for the specified stream, with the specified byte order mark detection option.
Public methodSupported by the XNA FrameworkStreamReader(Stream, Encoding)Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding.
Public methodSupported by the XNA FrameworkStreamReader(String, Boolean)Initializes a new instance of the StreamReader class for the specified file name, with the specified byte order mark detection option.
Public methodSupported by the XNA FrameworkStreamReader(String, Encoding)Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding.
Public methodSupported by the XNA FrameworkStreamReader(Stream, Encoding, Boolean)Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.
Public methodSupported by the XNA FrameworkStreamReader(String, Encoding, Boolean)Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection option.
Public methodSupported by the XNA FrameworkStreamReader(Stream, Encoding, Boolean, Int32)Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding, byte order mark detection option, and buffer size.
Public methodSupported by the XNA FrameworkStreamReader(String, Encoding, Boolean, Int32)Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding, byte order mark detection option, and buffer size.
Top
Methods

 NameDescription
Public methodSupported by the XNA FrameworkCloseCloses the StreamReader object and the underlying stream, and releases any system resources associated with the reader. (Overrides TextReader.Close().)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodSupported by the XNA FrameworkDiscardBufferedDataAllows a StreamReader object to discard its current data.
Public methodSupported by the XNA FrameworkDispose()Releases all resources used by the TextReader object. (Inherited from TextReader.)
Protected methodSupported by the XNA FrameworkDispose(Boolean)Closes the underlying stream, releases the unmanaged resources used by the StreamReader, and optionally releases the managed resources. (Overrides TextReader.Dispose(Boolean) .)
Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkFinalizeAllows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public methodSupported by the XNA FrameworkPeekReturns the next available character but does not consume it. (Overrides TextReader.Peek().)
Public methodSupported by the XNA FrameworkRead()Reads the next character from the input stream and advances the character position by one character. (Overrides TextReader.Read().)
Public methodSupported by the XNA FrameworkRead(Char[], Int32, Int32) Reads a maximum of count characters from the current stream into buffer, beginning at index. (Overrides TextReader.Read(Char[], Int32, Int32) .)
Public methodSupported by the XNA FrameworkReadBlockReads a maximum of count characters from the current stream, and writes the data to buffer, beginning at index. (Inherited from TextReader.)
Public methodSupported by the XNA FrameworkReadLineReads a line of characters from the current stream and returns the data as a string. (Overrides TextReader.ReadLine().)
Public methodSupported by the XNA FrameworkReadToEndReads the stream from the current position to the end of the stream. (Overrides TextReader.ReadToEnd().)
Public methodSupported by the XNA FrameworkToStringReturns a String that represents the current Object. (Inherited from Object.)
Top

BinaryReader MembersVisual Studio 2010

Reads primitive data types as binary values in a specific encoding.

The BinaryReader type exposes the following members.

Constructors

 NameDescription
Public methodSupported by the XNA FrameworkBinaryReader(Stream)Initializes a new instance of the BinaryReader class based on the supplied stream and using UTF8Encoding.
Public methodSupported by the XNA FrameworkBinaryReader(Stream, Encoding)Initializes a new instance of the BinaryReader class based on the supplied stream and a specific character encoding.
Top
Methods

 NameDescription
Public methodSupported by the XNA FrameworkCloseCloses the current reader and the underlying stream.
Public methodDispose()Releases all resources used by the current instance of the BinaryReader class.
Protected methodSupported by the XNA FrameworkDispose(Boolean)Releases the unmanaged resources used by the BinaryReader class and optionally releases the managed resources.
Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkFillBufferFills the internal buffer with the specified number of bytes read from the stream.
Protected methodSupported by the XNA FrameworkFinalizeAllows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkPeekCharReturns the next available character and does not advance the byte or character position.
Public methodSupported by the XNA FrameworkRead()Reads characters from the underlying stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.
Public methodSupported by the XNA FrameworkRead(Byte[], Int32, Int32) Reads the specified number of bytes from the stream, starting from a specified point in the byte array.
Public methodSupported by the XNA FrameworkRead(Char[], Int32, Int32) Reads the specified number of characters from the stream, starting from a specified point in the character array.
Protected methodSupported by the XNA FrameworkRead7BitEncodedIntReads in a 32-bit integer in compressed format.
Public methodSupported by the XNA FrameworkReadBooleanReads a Boolean value from the current stream and advances the current position of the stream by one byte.
Public methodSupported by the XNA FrameworkReadByteReads the next byte from the current stream and advances the current position of the stream by one byte.
Public methodSupported by the XNA FrameworkReadBytesReads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.
Public methodSupported by the XNA FrameworkReadCharReads the next character from the current stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.
Public methodSupported by the XNA FrameworkReadCharsReads the specified number of characters from the current stream, returns the data in a character array, and advances the current position in accordance with the Encoding used and the specific character being read from the stream.
Public methodSupported by the XNA FrameworkReadDecimalReads a decimal value from the current stream and advances the current position of the stream by sixteen bytes.
Public methodSupported by the XNA FrameworkReadDoubleReads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.
Public methodSupported by the XNA FrameworkReadInt16Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.
Public methodSupported by the XNA FrameworkReadInt32Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
Public methodSupported by the XNA FrameworkReadInt64Reads an 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.
Public methodSupported by the XNA FrameworkReadSByteReads a signed byte from this stream and advances the current position of the stream by one byte.
Public methodSupported by the XNA FrameworkReadSingleReads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.
Public methodSupported by the XNA FrameworkReadStringReads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.
Public methodSupported by the XNA FrameworkReadUInt16Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes.
Public methodSupported by the XNA FrameworkReadUInt32Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes.
Public methodSupported by the XNA FrameworkReadUInt64Reads an 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes.
Public methodSupported by the XNA FrameworkToStringReturns a String that represents the current Object. (Inherited from Object.)
Top

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值