Exemple #1
0
        /// <summary>
        /// Create a list of active drives matched to their volume labels
        /// </summary>
        /// <returns>Active drives, matched to labels, if possible</returns>
        /// <remarks>
        /// https://stackoverflow.com/questions/3060796/how-to-distinguish-between-usb-and-floppy-devices?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
        /// https://msdn.microsoft.com/en-us/library/aa394173(v=vs.85).aspx
        /// </remarks>
        public static List <Drive> CreateListOfDrives()
        {
            var drives = new List <Drive>();

            // Get the floppy drives
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                                                 "SELECT * FROM Win32_LogicalDisk");

                var collection = searcher.Get();
                foreach (ManagementObject queryObj in collection)
                {
                    uint?mediaType = (uint?)queryObj["MediaType"];
                    if (mediaType != null && ((mediaType > 0 && mediaType < 11) || (mediaType > 12 && mediaType < 22)))
                    {
                        char devId = queryObj["DeviceID"].ToString()[0];
                        drives.Add(Drive.Floppy(devId));
                    }
                }
            }
            catch
            {
                // No-op
            }

            // Get the optical disc drives
            List <Drive> discDrives = DriveInfo.GetDrives()
                                      .Where(d => d.DriveType == DriveType.CDRom)
                                      .Select(d => Drive.Optical(d.Name[0], (d.IsReady ? (string.IsNullOrWhiteSpace(d.VolumeLabel) ? "disc" : d.VolumeLabel) : Template.DiscNotDetected), d.IsReady))
                                      .ToList();

            // Add the two lists together and order
            drives.AddRange(discDrives);
            drives = drives.OrderBy(i => i.Letter).ToList();

            return(drives);
        }
Exemple #2
0
        /// <summary>
        /// Get the current system from drive
        /// </summary>
        /// <param name="drive"></param>
        /// <returns></returns>
        public static KnownSystem?GetKnownSystem(Drive drive)
        {
            // If drive or drive letter are provided, we can't do anything
            if (drive?.Letter == null)
            {
                return(null);
            }

            string drivePath = $"{drive.Letter}:\\";

            // If we can't read the media in that drive, we can't do anything
            if (!Directory.Exists(drivePath))
            {
                return(null);
            }

            // Sega Dreamcast
            if (File.Exists(Path.Combine(drivePath, "IP.BIN")))
            {
                return(KnownSystem.SegaDreamcast);
            }

            // Sega Mega-CD / Sega-CD
            if (File.Exists(Path.Combine(drivePath, "_BOOT", "IP.BIN")) ||
                File.Exists(Path.Combine(drivePath, "_BOOT", "SP.BIN")) ||
                File.Exists(Path.Combine(drivePath, "_BOOT", "SP_AS.BIN")) ||
                File.Exists(Path.Combine(drivePath, "FILESYSTEM.BIN")))
            {
                return(KnownSystem.SegaCDMegaCD);
            }

            // Sony PlayStation and Sony PlayStation 2
            if (File.Exists(Path.Combine(drivePath, "SYSTEM.CNF")))
            {
                // Check for either BOOT or BOOT2
                using (StreamReader reader = File.OpenText(Path.Combine(drivePath, "SYSTEM.CNF")))
                {
                    while (!reader.EndOfStream)
                    {
                        string line = reader.ReadLine();
                        if (line.Contains("BOOT2"))
                        {
                            return(KnownSystem.SonyPlayStation2);
                        }
                        else if (line.Contains("BOOT"))
                        {
                            return(KnownSystem.SonyPlayStation);
                        }
                    }
                }

                // If we have a weird disc, just assume PS1
                return(KnownSystem.SonyPlayStation);
            }

            // Sony PlayStation 4
            if (drive.VolumeLabel.Equals("PS4VOLUME", StringComparison.OrdinalIgnoreCase))
            {
                return(KnownSystem.SonyPlayStation4);
            }

            // V.Tech V.Flash / V.Smile Pro
            if (File.Exists(Path.Combine(drivePath, "0SYSTEM")))
            {
                return(KnownSystem.VTechVFlashVSmilePro);
            }

            // Default return
            return(KnownSystem.NONE);
        }
Exemple #3
0
        /// <summary>
        /// Get the current media type from drive letter
        /// </summary>
        /// <param name="drive"></param>
        /// <returns></returns>
        /// <remarks>
        /// https://stackoverflow.com/questions/11420365/detecting-if-disc-is-in-dvd-drive
        /// </remarks>
        public static MediaType?GetMediaType(Drive drive)
        {
            // Take care of the non-optical stuff first
            // TODO: See if any of these can be more granular, like Optical is
            if (drive.InternalDriveType == InternalDriveType.Floppy)
            {
                return(MediaType.FloppyDisk);
            }
            else if (drive.InternalDriveType == InternalDriveType.HardDisk)
            {
                return(MediaType.HardDisk);
            }
            else if (drive.InternalDriveType == InternalDriveType.Removable)
            {
                return(MediaType.FlashDrive);
            }

            // Get the DeviceID from the current drive letter
            string deviceId = null;

            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                                                 "SELECT * FROM Win32_CDROMDrive WHERE Id = '" + drive.Letter + ":\'");

                var collection = searcher.Get();
                foreach (ManagementObject queryObj in collection)
                {
                    deviceId = (string)queryObj["DeviceID"];
                }
            }
            catch
            {
                // We don't care what the error was
                return(null);
            }

            // If we got no valid device, we don't care and just return
            if (deviceId == null)
            {
                return(null);
            }

            // Get all relevant disc information
            try
            {
                MsftDiscMaster2 discMaster = new MsftDiscMaster2();
                deviceId = deviceId.ToLower().Replace('\\', '#');
                string id = null;
                foreach (var disc in discMaster)
                {
                    if (disc.ToString().Contains(deviceId))
                    {
                        id = disc.ToString();
                    }
                }

                // If we couldn't find the drive, we don't care and return
                if (id == null)
                {
                    return(null);
                }

                // Otherwise, we get the media type, if any
                MsftDiscRecorder2 recorder = new MsftDiscRecorder2();
                recorder.InitializeDiscRecorder(id);
                MsftDiscFormat2Data dataWriter = new MsftDiscFormat2Data();
                dataWriter.Recorder = recorder;
                var media = dataWriter.CurrentPhysicalMediaType;
                if (media != IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_UNKNOWN)
                {
                    return(Converters.ToMediaType(media));
                }
            }
            catch
            {
                // We don't care what the error is
            }

            return(null);
        }