public void cekDrive() { IDiscRecorder2 Recorder = (IDiscRecorder2)comboBoxDrive.Items[comboBoxDrive.SelectedIndex]; IDiscFormat2Data FormatData = null; try { FormatData = new MsftDiscFormat2Data(); if (!FormatData.IsRecorderSupported(Recorder)) { aktifkanUIData(false); aktifkanUIImage(false); } } catch { suaraGalat(true); MessageBox.Show(this, "Drive ini tidak mendukung Burning. \nSilakan pilih drive lainnya atau hubungi manufaktur komputer ini.", pesan, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { if (FormatData != null) { Marshal.ReleaseComObject(FormatData); } } }
/// <summary> /// Selected a new device /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void devicesComboBox_SelectedIndexChanged(object sender, EventArgs e) { System.Diagnostics.Debug.WriteLine("devicesComboBox_SelectedIndexChanged\n"); IDiscRecorder2 discRecorder = (IDiscRecorder2)devicesComboBox.Items[devicesComboBox.SelectedIndex]; supportedMediaLabel.Text = string.Empty; //mediaComboBox.Items.Clear(); //m_isCdromSupported = false; //m_isDvdSupported = false; //m_isDualLayerDvdSupported = false; //m_isBluraySupported = false; // // Verify recorder is supported // IDiscFormat2Data discFormatData = new MsftDiscFormat2Data(); if (!discFormatData.IsRecorderSupported(discRecorder)) { MessageBox.Show("Recorder not supported", m_clientName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } StringBuilder supportedMediaTypes = new StringBuilder(); foreach (IMAPI_MEDIA_PHYSICAL_TYPE mediaType in discFormatData.SupportedMediaTypes) { if (supportedMediaTypes.Length > 0) { supportedMediaTypes.Append(", "); } supportedMediaTypes.Append(GetMediaTypeString(mediaType)); } supportedMediaLabel.Text = supportedMediaTypes.ToString(); //// //// Add Media Selection //// //if (m_isCdromSupported) //{ // mediaComboBox.Items.Add(SIZE_CD); //} //if (m_isDvdSupported) //{ // mediaComboBox.Items.Add(SIZE_DVD); //} //if (m_isDualLayerDvdSupported) //{ // mediaComboBox.Items.Add(SIZE_DVDDL); //} //if (m_isBluraySupported) //{ // mediaComboBox.Items.Add(SIZE_BLURAY); //} //mediaComboBox.SelectedIndex = 0; }
/// <summary> /// Selected a new device /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void devicesComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (devicesComboBox.SelectedIndex == -1) { return; } var discRecorder = (IDiscRecorder2)devicesComboBox.Items[devicesComboBox.SelectedIndex]; supportedMediaLabel.Text = string.Empty; // // Verify recorder is supported // IDiscFormat2Data discFormatData = null; try { discFormatData = new MsftDiscFormat2Data(); if (!discFormatData.IsRecorderSupported(discRecorder)) { MessageBox.Show("Recorder not supported", ClientName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } StringBuilder supportedMediaTypes = new StringBuilder(); foreach (IMAPI_PROFILE_TYPE profileType in discRecorder.SupportedProfiles) { string profileName = GetProfileTypeString(profileType); if (string.IsNullOrEmpty(profileName)) { continue; } if (supportedMediaTypes.Length > 0) { supportedMediaTypes.Append(", "); } supportedMediaTypes.Append(profileName); } supportedMediaLabel.Text = supportedMediaTypes.ToString(); } catch (COMException) { supportedMediaLabel.Text = "Error getting supported types"; } finally { if (discFormatData != null) { Marshal.ReleaseComObject(discFormatData); } } }
/// <summary> /// Burns data files to disc in a single session using files from a /// single directory tree. /// </summary> /// <param name="recorder">Burning Device. Must be initialized.</param> /// <param name="path">Directory of files to burn.</param> public void BurnDirectory(IDiscRecorder2 recorder, String path) { // Define the new disc format and set the recorder IDiscFormat2Data dataWriterImage = new MsftDiscFormat2Data(); dataWriterImage.Recorder = recorder; if (!dataWriterImage.IsRecorderSupported(recorder)) { Console.WriteLine("The recorder is not supported"); return; } if (!dataWriterImage.IsCurrentMediaSupported(recorder)) { Console.WriteLine("The current media is not supported"); return; } dataWriterImage.ClientName = "IMAPI Sample"; // Create an image stream for a specified directory. // Create a new file system image and retrieve the root directory IFileSystemImage fsi = new MsftFileSystemImage(); // Set the media size fsi.FreeMediaBlocks = dataWriterImage.FreeSectorsOnMedia; // Use legacy ISO 9660 Format fsi.FileSystemsToCreate = FsiFileSystems.FsiFileSystemISO9660; // Add the directory to the disc file system IFsiDirectoryItem dir = fsi.Root; dir.AddTree(path, false); // Create an image from the file system Console.WriteLine("Writing content to disc..."); IFileSystemImageResult result = fsi.CreateResultImage(); // Data stream sent to the burning device IStream stream = result.ImageStream; DiscFormat2Data_Events progress = dataWriterImage as DiscFormat2Data_Events; progress.Update += new DiscFormat2Data_EventsHandler(DiscFormat2Data_ProgressUpdate); // Write the image stream to disc using the specified recorder. dataWriterImage.Write(stream); // Burn the stream to disc progress.Update -= new DiscFormat2Data_EventsHandler(DiscFormat2Data_ProgressUpdate); Console.WriteLine("----- Finished writing content -----"); }
private void DevicesComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (devicesComboBox.SelectedIndex == -1) { return; } var discRecorder = (IDiscRecorder2)devicesComboBox.Items[devicesComboBox.SelectedIndex]; IDiscFormat2Data discFormatData = null; try { discFormatData = new MsftDiscFormat2Data(); if (!discFormatData.IsRecorderSupported(discRecorder)) { MessageBox.Show("Recorder not supported", ClientName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } totalSpaceOnDisk = (2048 * discFormatData.TotalSectorsOnMedia) / 1048576; TotalSpace.Text = "Total Space: " + Convert.ToInt32(totalSpaceOnDisk).ToString() + " MB"; freeSpaceOnDisk = freeSpaceOnDisk == null ? (2048 * discFormatData.FreeSectorsOnMedia) / 1048576 : freeSpaceOnDisk + (2048 * discFormatData.FreeSectorsOnMedia) / 1048576; FreeSpace.Text = "Free Space: " + Convert.ToInt32(freeSpaceOnDisk) + " MB"; var supportedMediaTypes = new StringBuilder(); foreach (IMAPI_PROFILE_TYPE profileType in discRecorder.SupportedProfiles) { var profileName = _dictionares.ProfileTypeDictionary[profileType]; if (string.IsNullOrEmpty(profileName)) { continue; } if (supportedMediaTypes.Length > 0) { supportedMediaTypes.Append(", "); } supportedMediaTypes.Append(profileName); } } catch (COMException) { } finally { if (discFormatData != null) { Marshal.ReleaseComObject(discFormatData); } } }
/// <summary> /// Burns data files to disc in a single session using files from a /// single directory tree. /// </summary> /// <param name="recorder">Burning Device. Must be initialized.</param> /// <param name="path">Directory of files to burn.</param> public void BurnDirectory(IDiscRecorder2 recorder, String path) { // Define the new disc format and set the recorder IDiscFormat2Data dataWriterImage = new MsftDiscFormat2Data(); dataWriterImage.Recorder = recorder; if(!dataWriterImage.IsRecorderSupported(recorder)) { Console.WriteLine("The recorder is not supported"); return; } if (!dataWriterImage.IsCurrentMediaSupported(recorder)) { Console.WriteLine("The current media is not supported"); return; } dataWriterImage.ClientName = "IMAPI Sample"; // Create an image stream for a specified directory. // Create a new file system image and retrieve the root directory IFileSystemImage fsi = new MsftFileSystemImage(); // Set the media size fsi.FreeMediaBlocks = dataWriterImage.FreeSectorsOnMedia; // Use legacy ISO 9660 Format fsi.FileSystemsToCreate = FsiFileSystems.FsiFileSystemISO9660; // Add the directory to the disc file system IFsiDirectoryItem dir = fsi.Root; dir.AddTree(path, false); // Create an image from the file system Console.WriteLine("Writing content to disc..."); IFileSystemImageResult result = fsi.CreateResultImage(); // Data stream sent to the burning device IStream stream = result.ImageStream; DiscFormat2Data_Events progress = dataWriterImage as DiscFormat2Data_Events; progress.Update += new DiscFormat2Data_EventsHandler(DiscFormat2Data_ProgressUpdate); // Write the image stream to disc using the specified recorder. dataWriterImage.Write(stream); // Burn the stream to disc progress.Update -= new DiscFormat2Data_EventsHandler(DiscFormat2Data_ProgressUpdate); Console.WriteLine("----- Finished writing content -----"); }
/// <summary> /// 初始化Recorder /// 更新Recorder信息,更新光盘后可重试. /// </summary> public void InitRecorder() { try { if (msRecorder.VolumePathNames != null && msRecorder.VolumePathNames.Length > 0) { foreach (object mountPoint in msRecorder.VolumePathNames) { //挂载点 取其中一个 RecorderName = mountPoint.ToString(); break; } } // Define the new disc format and set the recorder MsftDiscFormat2Data dataWriter = new MsftDiscFormat2Data(); dataWriter.Recorder = msRecorder; if (!dataWriter.IsRecorderSupported(msRecorder)) { return; } if (!dataWriter.IsCurrentMediaSupported(msRecorder)) { return; } if (dataWriter.FreeSectorsOnMedia >= 0) { //可用大小 FreeDiskSize = dataWriter.FreeSectorsOnMedia * 2048L; } if (dataWriter.TotalSectorsOnMedia >= 0) { //总大小 TotalDiskSize = dataWriter.TotalSectorsOnMedia * 2048L; } CurMediaState = dataWriter.CurrentMediaStatus; //媒体状态 CurMediaStateName = RecorderHelper.GetMediaStateName(CurMediaState); CurMediaType = dataWriter.CurrentPhysicalMediaType; //媒介类型 CurMediaTypeName = RecorderHelper.GetMediaTypeName(CurMediaType); CanBurn = RecorderHelper.GetMediaBurnAble(CurMediaState); //判断是否可刻录 } catch (COMException ex) { string errMsg = ex.Message.Replace("\r\n", ""); //去掉异常信息里的\r\n this.CurMediaStateName = $"COM Exception:{errMsg}"; } catch (Exception ex) { this.CurMediaStateName = $"{ex.Message}"; } }
/// <summary> /// Set the recorder list. /// </summary> private void SetRecoderList() { // If recorders exist. if (_discMaster.Count > 0) { int count = 0; // Create the list of recorders. _discRecoders = new List <DiscRecorder>(); // For each recirder found. foreach (string device in _discMaster) { // Initialize the DiscRecorder object for the specified burning device. IDiscRecorder2 recorder = new MsftDiscRecorder2(); recorder.InitializeDiscRecorder(_discMaster[count]); // Get the media image. IDiscFormat2Data mediaImage = new MsftDiscFormat2Data(); mediaImage.Recorder = recorder; // Create a new recorder. DiscRecorder rec = new DiscRecorder() { Index = count, Name = device, Recorder = recorder, MediaImage = mediaImage, VolumeName = recorder.VolumeName, ProductId = recorder.ProductId, VendorId = recorder.VendorId, ActiveDiscRecorder = recorder.ActiveDiscRecorder, VolumePathNames = recorder.VolumePathNames, CurrentFeaturePages = recorder.CurrentFeaturePages, CurrentProfiles = recorder.CurrentProfiles, SupportedFeaturePages = recorder.SupportedFeaturePages, SupportedProfiles = recorder.SupportedProfiles, SupportedModePages = recorder.SupportedModePages, IsRecorderSupported = mediaImage.IsRecorderSupported(recorder), IsCurrentMediaSupported = mediaImage.IsCurrentMediaSupported(recorder), SupportedMediaTypes = mediaImage.SupportedMediaTypes }; // Add the recorder to the list. _discRecoders.Add(rec); count++; } } }
public static bool CanWriteMedia(IDiscRecorder2 rec) { var mi = new MsftDiscFormat2Data(); try { mi.Recorder = rec; return(mi.IsRecorderSupported(rec) && mi.IsCurrentMediaSupported(rec)); } catch { return(false); } finally { Marshal.FinalReleaseComObject(mi); } }
/// <summary> /// 获得光碟容量 /// </summary> private void GetDiskSize() { IDiscFormat2Data format2Data = null; try { format2Data = new MsftDiscFormat2Data(); if (format2Data.IsRecorderSupported(SelectedMediaWriter) == false) { this.Host.ShowMessageBox("没有光碟", MessageBoxActions.Ok); return; } if (!format2Data.IsCurrentMediaSupported(SelectedMediaWriter)) { this.Host.ShowMessageBox("没有光碟", MessageBoxActions.Ok); _diskSize = -1; _diskUseAbleSize = -1; return; } format2Data.Recorder = SelectedMediaWriter; IMAPI_MEDIA_PHYSICAL_TYPE mediaType = format2Data.CurrentPhysicalMediaType; CurrentMediaDescription = EnumeratorMediaPhysicalLType.GetMediaTypeString(mediaType); Int64 totalSectors = format2Data.TotalSectorsOnMedia; Int64 freeSectors = format2Data.FreeSectorsOnMedia; _diskSize = totalSectors * 2048; _diskUseAbleSize = freeSectors * 2048; } catch (COMException e) { this.Host.ShowMessageBox(ImapiReturnValues.GetName(e.ErrorCode), MessageBoxActions.Ok); _diskSize = 0; _diskUseAbleSize = 0; } finally { if (format2Data != null) { Marshal.ReleaseComObject(format2Data); } } }
public bool RecorderIsSupported(MsftDiscRecorder2 discRecorder) { IDiscFormat2Data discFormatData = null; bool answer = false; try { discFormatData = new MsftDiscFormat2Data(); answer = discFormatData.IsRecorderSupported(discRecorder); } finally { if (discFormatData != null) { Marshal.ReleaseComObject(discFormatData); } } return(answer); }
public void LoadRecorder() { if (_recorders.SelectedIndex == -1) { throw new InvalidOperationException("No DiscRecorder selected from the DiscRecorders list."); } MsftDiscRecorder2 recorder = null; IDiscFormat2Data discFormatData = null; try { recorder = new MsftDiscRecorder2(); recorder.InitializeDiscRecorder(_recorders.SelectedItem.InternalUniqueId); discFormatData = new MsftDiscFormat2Data(); switch (discFormatData.IsRecorderSupported(recorder)) { case 0x80004003: throw new RecorderNotSupportedException("Pointer is not valid."); case 0xC0AA0202: throw new RecorderNotSupportedException("The selected recorder is not supported on this system."); } _recorderLoaded = true; _mediaLoaded = false; } finally { if (discFormatData != null) { Marshal.ReleaseComObject(discFormatData); } if (recorder != null) { Marshal.ReleaseComObject(recorder); } } }
static BurnHelper() { var discMaster = new MsftDiscMaster2(); try { _recorders = new List <IDiscRecorder2>(); for (var i = 0; i < discMaster.Count; ++i) { IDiscFormat2Data mi = new MsftDiscFormat2Data(); try { IDiscRecorder2 rec = new MsftDiscRecorder2(); rec.InitializeDiscRecorder(discMaster[i]); //remove the comment for next 2 lines mi.Recorder = rec; if (mi.IsRecorderSupported(rec)) { _recorders.Add(rec); } } catch (Exception) { //throw; } finally { Marshal.FinalReleaseComObject(mi); } } } finally { Marshal.FinalReleaseComObject(discMaster); } }
public void LoadRecorder() { if (_recorders.SelectedIndex == -1) throw new InvalidOperationException("No DiscRecorder selected from the DiscRecorders list."); MsftDiscRecorder2 recorder = null; IDiscFormat2Data discFormatData = null; try { recorder = new MsftDiscRecorder2(); recorder.InitializeDiscRecorder(_recorders.SelectedItem.InternalUniqueId); discFormatData = new MsftDiscFormat2Data(); switch (discFormatData.IsRecorderSupported(recorder)) { case 0x80004003: throw new RecorderNotSupportedException("Pointer is not valid."); case 0xC0AA0202: throw new RecorderNotSupportedException("The selected recorder is not supported on this system."); } _recorderLoaded = true; _mediaLoaded = false; } finally { if (discFormatData != null) Marshal.ReleaseComObject(discFormatData); if (recorder != null) Marshal.ReleaseComObject(recorder); } }
/// <summary> /// Get the current media type from drive letter /// </summary> /// <param name="drive"></param> /// <returns></returns> /// <remarks> /// This may eventually be replaced by Aaru.Devices being able to be about 10x more accurate. /// This will also end up making it so that IMAPI2 is no longer necessary. Unfortunately, that /// will only work for .NET Core 3.1 and beyond. /// </remarks> public static (MediaType?, string) 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, null); } else if (drive.InternalDriveType == InternalDriveType.HardDisk) { return(MediaType.HardDisk, null); } else if (drive.InternalDriveType == InternalDriveType.Removable) { return(MediaType.FlashDrive, null); } // Get the current drive information string deviceId = null; bool loaded = false; try { // Get the device ID first var searcher = new ManagementObjectSearcher( "root\\CIMV2", $"SELECT * FROM Win32_CDROMDrive WHERE Id = '{drive.Letter}:\'"); foreach (ManagementObject queryObj in searcher.Get()) { deviceId = (string)queryObj["DeviceID"]; loaded = (bool)queryObj["MediaLoaded"]; } // If we got no valid device, we don't care and just return if (deviceId == null) { return(null, "Device could not be found"); } else if (!loaded) { return(null, "Device is not reporting media loaded"); } #if NET_FRAMEWORK MsftDiscMaster2 discMaster = new MsftDiscMaster2(); deviceId = deviceId.ToLower().Replace('\\', '#').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, "Device ID could not be found"); } // Create the required objects for reading from the drive MsftDiscRecorder2 recorder = new MsftDiscRecorder2(); recorder.InitializeDiscRecorder(id); MsftDiscFormat2Data dataWriter = new MsftDiscFormat2Data(); // If the recorder is not supported, just return if (!dataWriter.IsRecorderSupported(recorder)) { return(null, "IMAPI2 recorder not supported"); } // Otherwise, set the recorder to get information from dataWriter.Recorder = recorder; var media = dataWriter.CurrentPhysicalMediaType; return(media.IMAPIToMediaType(), null); #else // TODO: This entire .NET Core path still doesn't work // This may honestly require an entire import of IMAPI2 stuff and then try // as best as possible to get it working. return(null, "Media detection only supported on .NET Framework"); #endif } catch (Exception ex) { return(null, ex.Message); } }
/// <summary> /// Get the current media type from drive letter /// </summary> /// <param name="drive"></param> /// <returns></returns> /// <remarks> /// This may eventually be replaced by Aaru.Devices being able to be about 10x more accurate. /// This will also end up making it so that IMAPI2 is no longer necessary. Unfortunately, that /// will only work for .NET Core 3.1 and beyond. /// </remarks> public (MediaType?, string) GetMediaType() { // Take care of the non-optical stuff first // TODO: See if any of these can be more granular, like Optical is if (this.InternalDriveType == Data.InternalDriveType.Floppy) { return(MediaType.FloppyDisk, null); } else if (this.InternalDriveType == Data.InternalDriveType.HardDisk) { return(MediaType.HardDisk, null); } else if (this.InternalDriveType == Data.InternalDriveType.Removable) { return(MediaType.FlashDrive, null); } #if NET_FRAMEWORK // Get the current drive information string deviceId = null; bool loaded = false; try { // Get the device ID first var searcher = new ManagementObjectSearcher( "root\\CIMV2", $"SELECT * FROM Win32_CDROMDrive WHERE Id = '{this.Letter}:\'"); foreach (ManagementObject queryObj in searcher.Get()) { deviceId = (string)queryObj["DeviceID"]; loaded = (bool)queryObj["MediaLoaded"]; } // If we got no valid device, we don't care and just return if (deviceId == null) { return(null, "Device could not be found"); } else if (!loaded) { return(null, "Device is not reporting media loaded"); } MsftDiscMaster2 discMaster = new MsftDiscMaster2(); deviceId = deviceId.ToLower().Replace('\\', '#').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, "Device ID could not be found"); } // Create the required objects for reading from the drive MsftDiscRecorder2 recorder = new MsftDiscRecorder2(); recorder.InitializeDiscRecorder(id); MsftDiscFormat2Data dataWriter = new MsftDiscFormat2Data(); // If the recorder is not supported, just return if (!dataWriter.IsRecorderSupported(recorder)) { return(null, "IMAPI2 recorder not supported"); } // Otherwise, set the recorder to get information from dataWriter.Recorder = recorder; var media = dataWriter.CurrentPhysicalMediaType; return(media.IMAPIToMediaType(), null); } catch (Exception ex) { return(null, ex.Message); } #else try { var device = new AaruDevices.Device(this.Name); if (device.Error) { return(null, "Could not open device"); } else if (device.Type != DeviceType.ATAPI && device.Type != DeviceType.SCSI) { return(null, "Device does not support media type detection"); } // TODO: In order to get the disc type, Aaru.Core will need to be included as a // package. Unfortunately, it currently has a conflict with one of the required libraries: // System.Text.Encoding.CodePages (BOS uses >= 5.0.0, DotNetZip uses >= 4.5.0 && < 5.0.0) } catch (Exception ex) { return(null, ex.Message); } return(null, "Media detection only supported on .NET Framework"); #endif }
/// <summary> /// Selected a new device /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void devicesComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (devicesComboBox.SelectedIndex == -1) { return; } var discRecorder = (IDiscRecorder2)devicesComboBox.Items[devicesComboBox.SelectedIndex]; supportedMediaLabel.Text = string.Empty; // // Verify recorder is supported // IDiscFormat2Data discFormatData = null; try { discFormatData = new MsftDiscFormat2Data(); if (!discFormatData.IsRecorderSupported(discRecorder)) { MessageBox.Show("Recorder not supported", ClientName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } StringBuilder supportedMediaTypes = new StringBuilder(); foreach (IMAPI_PROFILE_TYPE profileType in discRecorder.SupportedProfiles) { string profileName = GetProfileTypeString(profileType); if (string.IsNullOrEmpty(profileName)) continue; if (supportedMediaTypes.Length > 0) supportedMediaTypes.Append(", "); supportedMediaTypes.Append(profileName); } supportedMediaLabel.Text = supportedMediaTypes.ToString(); } catch (COMException) { supportedMediaLabel.Text = "Error getting supported types"; } finally { if (discFormatData != null) { Marshal.ReleaseComObject(discFormatData); } } }
/// <summary> /// Examines and reports the media characteristics. /// </summary> /// <param name="recorder">Burning Device. Must be initialized.</param> public void DisplayMediaCharacteristics(IDiscRecorder2 recorder) { // Define the new disc format and set the recorder IDiscFormat2Data mediaImage = new MsftDiscFormat2Data(); mediaImage.Recorder = recorder; // *** Validation methods inherited from IMAPI2.MsftDiscFormat2 bool boolResult = mediaImage.IsRecorderSupported(recorder); if (boolResult) { Console.WriteLine("--- Current recorder IS supported. ---"); } else { Console.WriteLine("--- Current recorder IS NOT supported. ---"); } boolResult = mediaImage.IsCurrentMediaSupported(recorder); if (boolResult) { Console.WriteLine("--- Current media IS supported. ---"); } else { Console.WriteLine("--- Current media IS NOT supported. ---"); } Console.WriteLine("ClientName = {0}", mediaImage.ClientName); // Check a few CurrentMediaStatus possibilities. Each status is associated // with a bit and some combinations are legal. uint curMediaStatus = (uint)mediaImage.CurrentMediaStatus; Console.WriteLine("Checking Current Media Status"); if (curMediaStatus == (uint)IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_UNKNOWN) { Console.WriteLine("\tMedia state is unknown."); } else { if ((curMediaStatus & (uint)IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_OVERWRITE_ONLY) != 0) { Console.WriteLine("\tCurrently, only overwriting is supported."); } if ((curMediaStatus & (uint)IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_RANDOMLY_WRITABLE) != 0) { Console.WriteLine("\tCurrently, media supports random writing."); } if ((curMediaStatus & (uint)IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_APPENDABLE) != 0) { Console.WriteLine("\tMedia is currently appendable."); } if ((curMediaStatus & (uint)IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_FINAL_SESSION) != 0) { Console.WriteLine("\tMedia is in final writing session."); } if ((curMediaStatus & (uint)IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_DAMAGED) != 0) { Console.WriteLine("\tMedia is damaged."); } } IMAPI_MEDIA_PHYSICAL_TYPE mediaType = mediaImage.CurrentPhysicalMediaType; Console.Write("Current Media Type"); DisplayMediaType(mediaType); Console.WriteLine("SupportedMediaTypes in the device: "); foreach (IMAPI_MEDIA_PHYSICAL_TYPE supportedMediaType in mediaImage.SupportedMediaTypes) { DisplayMediaType(supportedMediaType); } Console.Write("\n----- Finished -----"); }
/// <summary> /// Selected a new device /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void devicesComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (devicesComboBox.SelectedIndex == -1) { return; } IDiscRecorder2 discRecorder = (IDiscRecorder2)devicesComboBox.Items[devicesComboBox.SelectedIndex]; supportedMediaLabel.Text = string.Empty; // // Verify recorder is supported // IDiscFormat2Data discFormatData = null; try { discFormatData = new MsftDiscFormat2Data(); if (!discFormatData.IsRecorderSupported(discRecorder)) { MessageBox.Show("Данное устройство записи не поддерживается", m_clientName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } StringBuilder supportedMediaTypes = new StringBuilder(); foreach (IMAPI_MEDIA_PHYSICAL_TYPE mediaType in discFormatData.SupportedMediaTypes) { if (supportedMediaTypes.Length > 0) supportedMediaTypes.Append(", "); supportedMediaTypes.Append(GetMediaTypeString(mediaType)); } supportedMediaLabel.Text = supportedMediaTypes.ToString(); } catch (COMException) { supportedMediaLabel.Text = "Ошибка получения поддерживаемых форматов"; } finally { if (discFormatData != null) { Marshal.ReleaseComObject(discFormatData); } } }
/// <summary> /// Get the media type for a device path using the Aaru libraries /// </summary> /// <param name="devicePath">Path to the device</param> /// <param name="internalDriveType">Current internal drive type</param> /// <returns>MediaType, null on error</returns> private static (MediaType?, string) GetMediaType(string devicePath, InternalDriveType?internalDriveType) { char driveLetter = devicePath == null || !devicePath.Any() ? '\0' : devicePath[0]; // Take care of the non-optical stuff first // TODO: See if any of these can be more granular, like Optical is if (internalDriveType == Data.InternalDriveType.Floppy) { return(MediaType.FloppyDisk, null); } else if (internalDriveType == Data.InternalDriveType.HardDisk) { return(MediaType.HardDisk, null); } else if (internalDriveType == Data.InternalDriveType.Removable) { return(MediaType.FlashDrive, null); } // Get the current drive information string deviceId = null; bool loaded = false; try { // Get the device ID first var searcher = new ManagementObjectSearcher( "root\\CIMV2", $"SELECT * FROM Win32_CDROMDrive WHERE Id = '{driveLetter}:\'"); foreach (ManagementObject queryObj in searcher.Get()) { deviceId = (string)queryObj["DeviceID"]; loaded = (bool)queryObj["MediaLoaded"]; } // If we got no valid device, we don't care and just return if (deviceId == null) { return(null, "Device could not be found"); } else if (!loaded) { return(null, "Device is not reporting media loaded"); } MsftDiscMaster2 discMaster = new MsftDiscMaster2(); deviceId = deviceId.ToLower().Replace('\\', '#').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, "Device ID could not be found"); } // Create the required objects for reading from the drive MsftDiscRecorder2 recorder = new MsftDiscRecorder2(); recorder.InitializeDiscRecorder(id); MsftDiscFormat2Data dataWriter = new MsftDiscFormat2Data(); // If the recorder is not supported, just return if (!dataWriter.IsRecorderSupported(recorder)) { return(null, "IMAPI2 recorder not supported"); } // Otherwise, set the recorder to get information from dataWriter.Recorder = recorder; var media = dataWriter.CurrentPhysicalMediaType; return(media.IMAPIToMediaType(), null); } catch (Exception ex) { return(null, ex.Message); } }
private string GetSupportedMediaTypes() { if (discRecorder == null) return null; // Verify recorder is supported IDiscFormat2Data discFormatData = null; try { discFormatData = new MsftDiscFormat2Data(); if (!discFormatData.IsRecorderSupported(discRecorder)) return "Recorder not supported"; var supportedMediaTypes = new StringBuilder(); foreach (IMAPI_PROFILE_TYPE profileType in discRecorder.SupportedProfiles) { string profileName = StringProvider.GetProfileTypeString(profileType); if (string.IsNullOrEmpty(profileName)) continue; if (supportedMediaTypes.Length > 0) supportedMediaTypes.Append(", "); supportedMediaTypes.Append(profileName); } return supportedMediaTypes.ToString(); } catch (COMException) { return "Error getting supported types"; } finally { if (discFormatData != null) Marshal.ReleaseComObject(discFormatData); } }