C# 获取磁盘信息

namespace ConsoleApp_DriveInfo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Driveinfo();
        }

        static void Driveinfo()
        {

            DriveInfo driveInfo = new DriveInfo("d:/");
            

            // [ 属性 ]
            Console.WriteLine(driveInfo.DriveType);  // 获取磁盘类型
            Console.WriteLine(driveInfo.Name);       // 磁盘盘符
            Console.WriteLine(driveInfo.VolumeLabel);   // 磁盘名称
            Console.WriteLine(((driveInfo.TotalSize/1024)/1024)/1024+"GB");         // 获取磁盘总容量
            Console.WriteLine(((driveInfo.TotalFreeSpace/1024)/1024)/1024+"GB");    // 获取磁盘可用空间            
            Console.WriteLine(driveInfo.DriveFormat);       // 磁盘格式

            foreach (var x in DriveInfo.GetDrives()) { Console.WriteLine(x); }  // 获取所有盘符
        }

    }
}

 

posted @ 2024-03-24 08:22  edolf  阅读(136)  评论(0编辑  收藏  举报