Latest web development tutorials

Operation C # Windows file system

Input and Output C # file Input and Output C # file

C # allows you to use a variety of directories and files related classes to manipulate directories and files, such asDirectoryInfo class and FileInfoclass.

DirectoryInfo class

DirectoryInfo class is derived from FileSystemInfoclass. It offers a variety used to create, move, browse the directory and subdirectories methods. This class can not be inherited.

The following table lists theDirectoryInfo class some common attributes:

序号属性 & 描述
1Attributes
获取当前文件或目录的属性。
2CreationTime
获取当前文件或目录的创建时间。
3Exists
获取一个表示目录是否存在的布尔值。
4Extension
获取表示文件存在的字符串。
5FullName
获取目录或文件的完整路径。
6LastAccessTime
获取当前文件或目录最后被访问的时间。
7Name
获取该 DirectoryInfo 实例的名称。

The following table lists some of theDirectoryInfo class commonly used methods:

序号方法 & 描述
1public void Create()
创建一个目录。
2public DirectoryInfo CreateSubdirectory( string path )
在指定的路径上创建子目录。指定的路径可以是相对于 DirectoryInfo 类的实例的路径。
3public override void Delete()
如果为空的,则删除该 DirectoryInfo。
4public DirectoryInfo[] GetDirectories()
返回当前目录的子目录。
5public FileInfo[] GetFiles()
从当前目录返回文件列表。

For a complete list of properties and methods, visit the Microsoft C # documents.

FileInfo class

FileInfo class is derived from FileSystemInfoclass. It provides for creating, copying, deleting, moving, open the properties and methods of files and helps create a FileStream object. This class can not be inherited.

The following table lists theFileInfo class some common attributes:

序号属性 & 描述
1Attributes
获取当前文件的属性。
2CreationTime
获取当前文件的创建时间。
3Directory
获取文件所属目录的一个实例。
4Exists
获取一个表示文件是否存在的布尔值。
5Extension
获取表示文件存在的字符串。
6FullName
获取文件的完整路径。
7LastAccessTime
获取当前文件最后被访问的时间。
8LastWriteTime
获取文件最后被写入的时间。
9Length
获取当前文件的大小,以字节为单位。
10Name
获取文件的名称。

The following table lists some of theFileInfo class commonly used methods:

序号方法 & 描述
1public StreamWriter AppendText()
创建一个 StreamWriter,追加文本到由 FileInfo 的实例表示的文件中。
2public FileStream Create()
创建一个文件。
3public override void Delete()
永久删除一个文件。
4public void MoveTo( string destFileName )
移动一个指定的文件到一个新的位置,提供选项来指定新的文件名。
5public FileStream Open( FileMode mode )
以指定的模式打开一个文件。
6public FileStream Open( FileMode mode, FileAccess access )
以指定的模式,使用 read、write 或 read/write 访问,来打开一个文件。
7public FileStream Open( FileMode mode, FileAccess access, FileShare share )
以指定的模式,使用 read、write 或 read/write 访问,以及指定的分享选项,来打开一个文件。
8public FileStream OpenRead()
创建一个只读的 FileStream。
9public FileStream OpenWrite()
创建一个只写的 FileStream。

For a complete list of properties and methods, visit the Microsoft C # documents.

Examples

The following example demonstrates the above-mentioned categories of usage:

using System;
using System.IO;

namespace WindowsFileApplication
{
    class Program
    {
        static void Main (string [] args)
        {
            // Create a DirectoryInfo object DirectoryInfo mydir = new DirectoryInfo (@ "c: \ Windows");

            // Get the files in the directory as well as their names and the size of FileInfo [] f = mydir.GetFiles ();
            foreach (FileInfo file in f)
            {
                Console.WriteLine ( "File Name: {0} Size: {1}",
				    file.Name, file.Length);
            }
            Console.ReadKey ();
        }
    }
}

When you compile and execute the above program, it will display the file name and their size in Windows directory.

Input and Output C # file Input and Output C # file