Operation C # Windows file system
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:
序号 | 属性 & 描述 |
---|---|
1 | Attributes 获取当前文件或目录的属性。 |
2 | CreationTime 获取当前文件或目录的创建时间。 |
3 | Exists 获取一个表示目录是否存在的布尔值。 |
4 | Extension 获取表示文件存在的字符串。 |
5 | FullName 获取目录或文件的完整路径。 |
6 | LastAccessTime 获取当前文件或目录最后被访问的时间。 |
7 | Name 获取该 DirectoryInfo 实例的名称。 |
The following table lists some of theDirectoryInfo class commonly used methods:
序号 | 方法 & 描述 |
---|---|
1 | public void Create() 创建一个目录。 |
2 | public DirectoryInfo CreateSubdirectory(
string path
)
在指定的路径上创建子目录。指定的路径可以是相对于 DirectoryInfo 类的实例的路径。 |
3 | public override void Delete() 如果为空的,则删除该 DirectoryInfo。 |
4 | public DirectoryInfo[] GetDirectories() 返回当前目录的子目录。 |
5 | public 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:
序号 | 属性 & 描述 |
---|---|
1 | Attributes 获取当前文件的属性。 |
2 | CreationTime 获取当前文件的创建时间。 |
3 | Directory 获取文件所属目录的一个实例。 |
4 | Exists 获取一个表示文件是否存在的布尔值。 |
5 | Extension 获取表示文件存在的字符串。 |
6 | FullName 获取文件的完整路径。 |
7 | LastAccessTime 获取当前文件最后被访问的时间。 |
8 | LastWriteTime 获取文件最后被写入的时间。 |
9 | Length 获取当前文件的大小,以字节为单位。 |
10 | Name 获取文件的名称。 |
The following table lists some of theFileInfo class commonly used methods:
序号 | 方法 & 描述 |
---|---|
1 | public StreamWriter AppendText() 创建一个 StreamWriter,追加文本到由 FileInfo 的实例表示的文件中。 |
2 | public FileStream Create() 创建一个文件。 |
3 | public override void Delete() 永久删除一个文件。 |
4 | public void MoveTo(
string destFileName
)
移动一个指定的文件到一个新的位置,提供选项来指定新的文件名。 |
5 | public FileStream Open(
FileMode mode
)
以指定的模式打开一个文件。 |
6 | public FileStream Open(
FileMode mode,
FileAccess access
)
以指定的模式,使用 read、write 或 read/write 访问,来打开一个文件。 |
7 | public FileStream Open(
FileMode mode,
FileAccess access,
FileShare share
)
以指定的模式,使用 read、write 或 read/write 访问,以及指定的分享选项,来打开一个文件。 |
8 | public FileStream OpenRead() 创建一个只读的 FileStream。 |
9 | public 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.