C# Driveinfo: Get computer drive information

Why do you need file operations?

These values ​​of variables and constants in the code are all stored in the memory, and all the data used when the program runs are deleted. If you need to save the data in the application for a long time, you can choose a file or database to store it. The file is usually stored in a designated location on the computer disk, and can be in the form of a notepad, a Word document, or a picture. Corresponding classes are provided in the C# language to directly implement operations such as file creation, movement, reading and writing in the program. File operation classes are in the System.IO namespace, including Driveinfo class, Directory class, Directoryinfo class, File class, Filelnfo class, Path class, etc.

C# Driveinfo: Get computer drive information

Viewing computer drive information mainly includes viewing disk space, disk file format, disk volume label, etc. In C# language, these operations can be implemented through the Driveinfo class.

The Driveinfo class is a sealed class, that is, it cannot be inherited.
The syntax is as follows:

Driveinfo driveInfo=new Driveinfo("C");
Attribute or method effect
AvailableFreeSpace Read-only attribute, get the amount of free space available on the drive (in bytes)
DriveFormat Read-only attribute, get the name of the file system format, such as NTFS or FAT32
DriveType Read-only attribute to get the type of drive, such as CD-ROM, removable drive, network drive or fixed drive
IsReady Read-only attribute, get a value indicating whether the drive is ready, True means ready, False means not ready
Name Read-only attribute, get the name of the drive, such as C:\
RootDirectory Read-only attribute, get the root directory of the drive
TotalFreeSpace Read-only attribute, get the total amount of free space available on the drive (in bytes)
TotalSize Read-only attribute, get the total size of storage space on the drive (in bytes)
VolumeLabel Properties, get or set the volume label of the drive
Driveinfo[] GetDrives() Static method to retrieve the drive names of all logical drives on the computer

example:

using System;
using System.IO;


namespace Myspace
{
    
    

    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            DriveInfo driveInfo = new DriveInfo("D");
            Console.WriteLine("驱动器的名称:" + driveInfo.Name);
            Console.WriteLine("驱动器类型:" + driveInfo.DriveType);
            Console.WriteLine("驱动器的文件格式:" + driveInfo.DriveFormat);
            Console.WriteLine("驱动器中可用空间大小:" 
                + driveInfo.TotalFreeSpace);
            Console.WriteLine("驱动器总大小:" + driveInfo.TotalSize);

            Console.ReadKey();
        }
        
    }

}

Operation result:
Insert picture description here
example:

using System;
using System.IO;

namespace Myspace
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            //检索计算机上所有逻辑驱动器的名称放到数组driveInfo中
            DriveInfo[] driveInfo = DriveInfo.GetDrives();
            //遍历数组
            foreach(DriveInfo d in driveInfo)
            {
    
    
                //指示驱动器是否准备好
                if(d.IsReady)
                {
    
    
                    Console.WriteLine("驱动器名称: " + d.Name);
                    Console.WriteLine("驱动器的文件格式 " 
                        + d.DriveFormat);
                }
            }
        }
    }
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_50188452/article/details/115014298