How to determine the SATA channel for a given drive?

Using the DISKPART command line DISKPART , I can get something called the “Location Path” that seems to give me what I need, you can view it using the detail disk command after selecting one of your disks in DISKPART .

It seems I can get this information programmatically through this class: MSFT_Disk

I am not sure how to get an instance of this class. I have several examples of using the ManagementObjectSearcher classes for WMI , but this method does not work for me, I am also not sure about the availability of MSFT_Disk in Windows 7, since the page mentions that it is for Windows 8.

Does anyone know a good way to get SATA channel information or the "location path" on disk?

+6
source share
3 answers

If you do not want to use Windows 8, I believe that WMI is a way:

 using System; using System.Linq; using System.Management; namespace DiskScanPOC { class Program { static void Main() { var managementScope = new ManagementScope(); //get disk drives var query = new ObjectQuery("select * from Win32_DiskDrive"); var searcher = new ManagementObjectSearcher(managementScope, query); var oReturnCollection = searcher.Get(); //List all properties available, in case the below isn't what you want. var colList = oReturnCollection.Cast<ManagementObject>().First(); foreach (var property in colList.Properties) { Console.WriteLine("Property: {0} = {1}", property.Name, property.Value); } //loop through found drives and write out info foreach (ManagementObject oReturn in oReturnCollection) { Console.WriteLine("Name : " + oReturn["Name"]); Console.WriteLine("Target Id: " + oReturn["SCSITargetId"]); Console.WriteLine("Port: " + oReturn["SCSIPort"]); } Console.Read(); } } } 

I did not open my case to check the SATA port numbers, but the above application looks like it gives reasonable results on my machine with 3 SATA hard drives.

0
source

If you want a location path, SetupDiGetDeviceRegistryProperty is the function you are looking for. Set the value of the SPDRP_LOCATION_INFORMATION property.

I assume that you already know how to list devices to receive DeviceInfoSet and DeviceInfoData .

0
source
 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Management; namespace Hard_Disk_Interface { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCheck_Click(object sender, EventArgs e) { WqlObjectQuery q = new WqlObjectQuery("SELECT * FROM Win32_IDEController"); ManagementObjectSearcher res = new ManagementObjectSearcher(q); lblHDDChanels.Text = string.Empty; foreach (ManagementObject o in res.Get()) { string Caption = o["Caption"].ToString(); lblHDDChanels.Text += Caption + "\n\n"; if (Caption.Contains("Serial")) { lblInterface.Text = "S-ATA"; } } } } } 

This is a demo ...

Note. First add the link System.Management.dll.net freamwork 4.0

0
source

Source: https://habr.com/ru/post/923004/


All Articles