Latest web development tutorials

Input and Output C # file

Afile is a data storage with the specified name and directory path set in the disk.When the file is opened for reading and writing, it becomes astream.

Fundamentally speaking, stream is a sequence of bytes transmitted through a communication path. There are two main streams:input and output streams.Input stream for reading data (read) from the file,the output streamfor writing data to a file (write).

C # I / O type

System.IO namespace have a variety of classes for performing various file operations, such as creating and deleting files, reading or writing files, closing files.

The following table lists some of the System.IO namespace commonly used non-abstract class:

I/O 类描述
BinaryReader从二进制流读取原始数据。
BinaryWriter以二进制格式写入原始数据。
BufferedStream字节流的临时存储。
Directory有助于操作目录结构。
DirectoryInfo用于对目录执行操作。
DriveInfo提供驱动器的信息。
File有助于处理文件。
FileInfo用于对文件执行操作。
FileStream用于文件中任何位置的读写。
MemoryStream用于随机访问存储在内存中的数据流。
Path对路径信息执行操作。
StreamReader用于从字节流中读取字符。
StreamWriter用于向一个流中写入字符。
StringReader用于读取字符串缓冲区。
StringWriter用于写入字符串缓冲区。

FileStream Class

System.IO namespaceFileStream class to read and write and help close the file.This class is derived from the abstract class Stream.

You need to create aFileStream object to create a new file, or open an existing file.Creates aFileStream object syntax is as follows:

FileStream <object_name> = new FileStream (<file_name>,
<FileMode Enumerator>, <FileAccess Enumerator>, <FileShare Enumerator>);

For example, create a FileStream object to read the file namedF sample.txtof:

FileStream F = new FileStream ( "sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
参数描述
FileMode

FileMode枚举定义了各种打开文件的方法。FileMode 枚举的成员有:

  • Append:打开一个已有的文件,并将光标放置在文件的末尾。如果文件不存在,则创建文件。
  • Create:创建一个新的文件。如果文件已存在,则删除旧文件,然后创建新文件。
  • CreateNew:指定操作系统应创建一个新的文件。如果文件已存在,则抛出异常。
  • Open:打开一个已有的文件。如果文件不存在,则抛出异常。
  • OpenOrCreate:指定操作系统应打开一个已有的文件。如果文件不存在,则用指定的名称创建一个新的文件打开。
  • Truncate:打开一个已有的文件,文件一旦打开,就将被截断为零字节大小。然后我们可以向文件写入全新的数据,但是保留文件的初始创建日期。如果文件不存在,则抛出异常。
FileAccess

FileAccess枚举的成员有:ReadReadWriteWrite

FileShare

FileShare枚举的成员有:

  • Inheritable:允许文件句柄可由子进程继承。Win32 不直接支持此功能。
  • None:谢绝共享当前文件。文件关闭前,打开该文件的任何请求(由此进程或另一进程发出的请求)都将失败。
  • Read:允许随后打开文件读取。如果未指定此标志,则文件关闭前,任何打开该文件以进行读取的请求(由此进程或另一进程发出的请求)都将失败。但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。
  • ReadWrite:允许随后打开文件读取或写入。如果未指定此标志,则文件关闭前,任何打开该文件以进行读取或写入的请求(由此进程或另一进程发出)都将失败。但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。
  • Write:允许随后打开文件写入。如果未指定此标志,则文件关闭前,任何打开该文件以进行写入的请求(由此进程或另一进过程发出的请求)都将失败。但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。
  • Delete:允许随后删除文件。

Examples

The following program demonstrates theFileStream class usage:

using System;
using System.IO;

namespace FileIOApplication
{
    class Program
    {
        static void Main (string [] args)
        {
            FileStream F = new FileStream ( "test.dat", 
            FileMode.OpenOrCreate, FileAccess.ReadWrite);

            for (int i = 1; i <= 20; i ++)
            {
                F.WriteByte ((byte) i);
            }

            F.Position = 0;

            for (int i = 0; i <= 20; i ++)
            {
                Console.Write (F.ReadByte () + "");
            }
            F.Close ();
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

1,234,567,891,011,121,314 151617181920-1

C # Advanced file operations

The above example demonstrates a simple C # file operations. However, to take full advantage of C # System.IO class of powerful, you need to know these common classes of properties and methods.

In the following sections, we will discuss these classes and the operations they perform. Click on the link to learn more about the various parts of knowledge:

主题描述
文本文件的读写 它涉及到文本文件的读写。StreamReaderStreamWriter类有助于完成文本文件的读写。
二进制文件的读写 它涉及到二进制文件的读写。BinaryReaderBinaryWriter类有助于完成二进制文件的读写。
Windows 文件系统的操作 它让 C# 程序员能够浏览并定位 Windows 文件和目录。