1.46. C# operation of Windows file system

发布时间 : 2025-10-25 13:34:32 UTC      

Page Views: 10 views

C# allows you to manipulate directories and files using classes related to various directories and files, such as DirectoryInfo class and FileInfo class.

1.46.1. DirectoryInfo class #

DirectoryInfo class is derived from the FileSystemInfo . It provides various methods for creating, moving and browsing directories and subdirectories. This class cannot be inherited.

The following table lists some DirectoryInfo commonly used properties inthe class:

Serial number

Attribute & description

1

Attributes gets the properties of the current file or directory.

2

CreationTime gets the creation time of the current file or directory.

3

Exists gets a Boolean value indicating whether the directory exists.

4

Extension gets a string that represents the existence of the file.

5

FullName gets the full path to a directory or file.

6

LastAccessTime gets when the current file or directory was last accessed.

7

Name gets the name of the DirectoryInfo instance.

The following table lists some DirectoryInfo commonly used methods:

Serial number

Method and description

1

Public void Create () creates a directory.

2

Public DirectoryInfo CreateSubdirectory (string path) creates a subdirectoryon the specified path. The specified path can be a path relative to an instance of the DirectoryInfo class.

3

Public override void Delete () deletes the DirectoryInfo if it is empty.

4

Public DirectoryInfo [] GetDirectories () returns a subdirectory of the current directory.

5

Public FileInfo [] GetFiles () returns a list of files from the current directory.

For a complete list of properties and methods, please visit Microsoft’s C # documentation.

1.46.2. FileInfo class #

FileInfo class is derived from the FileSystemInfo . It provides properties and methods for creating, copying, deleting, moving andopening files, and helps FileStream with creation of objects. This class cannot be inherited.

The following table lists some FileInfo commonly used properties:

Serial number

Attribute & description

1

Attributes gets the properties of the current file.

2

CreationTime gets the creation time of the current file.

3

Directory gets an instance of the directory to which the file belongs.

4

Exists gets a Boolean value indicating whether the file exists.

5

Extension gets a string that represents the existence of the file.

6

FullName gets the full path to the file.

7

LastAccessTime gets when the current file was last accessed.

8

LastWriteTime gets the time when the file was last written.

9

Length gets the size of the current file, in bytes.

10

Name gets the name of the file.

The following table lists some FileInfo commonly used methods:

Serial number

Method and description

1

Public StreamWriter AppendText () creates a StreamWriter and appends text tothe file represented by an instance of FileInfo.

2

Public FileStream Create () creates a file.

3

Public override void Delete () permanently deletes a file.

4

Public void MoveTo (string destFileName) moves a specified file to a new location, providing options to specify a new file name.

5

Public FileStream Open (FileMode mode) opens a file in the specified mode.

6

Public FileStream Open (FileMode mode, FileAccess access) opens a file usingread, write, or read/write access in the specified mode.

7

Public FileStream Open (FileMode mode, FileAccess access, FileShare share) opens a file in the specified mode, using read, write, or read/write access,as well as specified sharing options.

8

Public FileStream OpenRead () creates a read-only FileStream.

9

Public FileStream OpenWrite () creates a write-only FileStream.

For a complete list of properties and methods, please visit Microsoft’s C # documentation.

1.46.3. Example #

The following example demonstrates the use of the classes mentioned above:

using System; using System.IO; namespace WindowsFileApplication { class Program { static void Main(string[] args) { // Create a DirectoryInfo object DirectoryInfo mydir = new DirectoryInfo(@"c:\Windows"); // Obtain the files in the directory and their names and sizes 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 displays the names of thefiles and their size in the Windows directory.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。