c# 监测U盘(定时监测)

这个只引用System.IO

感觉有点没节操,就是定时执行 DriveInfo.GetDrives();
但是架不住简单不是吗!也是个思路

 DriveInfo[] s = DriveInfo.GetDrives();
  foreach (DriveInfo i in s)
        {
            string name = i.Name;
            Console.WriteLine($"设备名称:{name}\n\t设备类型:{i.DriveType}");
            if (i.DriveType == DriveType.Removable)
            {
                long freespace = i.TotalFreeSpace;//空闲空间大小
                long maxspace = i.TotalSize;//存储空间大小
                i.VolumeLabel = "h";//获取或设置驱动器的卷标。
                string VolumeLabel = i.VolumeLabel;
                 Console.WriteLine("发现U盘或移动硬盘");
            }
        }

直接上例子:

public static void Main(string[] args)
   {
        Timer timer = new Timer(10*1000);
        timer.Elapsed += Timer_Elapsed;
        timer.Enabled = true;
        timer.Start();
       Console.ReadKey();
    }
   private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        DriveInfo[] s = DriveInfo.GetDrives();
        uint flag = 0;
        foreach (DriveInfo i in s)
        {
            string name = i.Name;
            //Console.WriteLine($"设备名称:{name}\n\t设备类型:{i.DriveType}");
            if (i.DriveType == DriveType.Removable)
            {
             flag++;
             long freespace = i.TotalFreeSpace/(1024*1024 * 1024);//空闲空间大小
             long maxspace = i.TotalSize/ (1024 * 1024 * 1024);//存储空间大小
             i.VolumeLabel = "h";//获取或设置驱动器的卷标。
             string VolumeLabel = i.VolumeLabel;
             Console.WriteLine($"发现U盘或移动硬盘{VolumeLabel}:总存储{maxspace}GB,空闲{freespace}GB");
           }
        }
        if(flag<=0)
       Console.WriteLine("未发现U盘或移动硬盘");
        flag = 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值