1.46. C# operation of Windows file system

发布时间 :2023-10-12 23:00:07 UTC      

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.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.