Ejemplo n.º 1
0
 public FileSystemDrive(string name, string label, FileSystemDriveType type)
 {
     Name     = name;
     IsFolder = true;
     Label    = label;
     Type     = type;
 }
Ejemplo n.º 2
0
        public static IEnumerable <FileSystemDrive> GetFileSystemDrives()
        {
            DataTable drivesTable = WMIHandler.GetTable("Win32_LogicalDisk", "Name", "DriveType", "ProviderName", "VolumeName");

            foreach (DataRow row in drivesTable.Rows)
            {
                int driveType            = int.Parse(row["DriveType"].ToString());
                FileSystemDriveType type = Enum.IsDefined(typeof(FileSystemDriveType), driveType) ? (FileSystemDriveType)driveType : FileSystemDriveType.Unknown;
                string providerName      = row["ProviderName"]?.ToString().TrimEnd(Path.DirectorySeparatorChar) ?? string.Empty;
                string volumeName        = row["VolumeName"]?.ToString() ?? string.Empty;
                string label             = string.Empty;
                if (type == FileSystemDriveType.NetworkDrive && !string.IsNullOrEmpty(providerName))
                {
                    string labelPart = Path.GetFileName(providerName);
                    if (labelPart != null)
                    {
                        label = labelPart + " (" + providerName.Substring(0, providerName.Length - labelPart.Length).TrimEnd(Path.DirectorySeparatorChar) + ")";
                    }
                }
                else
                {
                    label = !string.IsNullOrEmpty(volumeName) ? volumeName : string.Empty;
                }

                yield return(new FileSystemDrive(row["Name"].ToString(), label, type));
            }
        }