C# 获取系统盘符

Posted on 2020-03-19 12:09  云起  阅读(47)  评论(0编辑  收藏  举报  来源

1、使用.net管理对象(引入System.Management)

        public static List<string> getDisk()
        {
            WqlObjectQuery wmiquery = new WqlObjectQuery("select * from Win32_LogiCalDisk");
            ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery);
            ManagementObjectCollection queryCollection = wmifind.Get();
            List<string> ls = new List<string>();
            foreach (var disk in queryCollection)
            {
                ls.Add(disk["DeviceID"].ToString());
            }
            return ls;
        }

2、使用驱动信息(引入System.IO)

        public static List<string> getDisk()
        {
            var drivers = DriveInfo.GetDrives();
            List<string> ls = new List<string>();
            foreach (var driver in drivers)
            {
               

Copyright © 2024 云起
Powered by .NET 9.0 on Kubernetes