C# operation of Windows file system


Release date:2023-09-02 Update date:2023-10-13 Editor:admin View counts:182

Label:

C# operation of Windows file system

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

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.

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.

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.

Powered by TorCMS (https://github.com/bukun/TorCMS).