How can I determine the SATA channel for a given disk?

3.3k views Asked by At

Using DISKPART command line utility, I can get something called a "Location path" which appears to give me what I need, you can view this by using the command detail disk after selecting one of your disks in diskpart.

It appears I can get this information programatically via this class: MSFT_Disk

I am unsure about how to get an instance of this class. I have a few examples of using a ManagementObjectSearcher for WMI classes but that method is not working for me, I am also unsure of MSFT_Disk's availability in Windows 7 as the page mentions that this is for Windows 8.

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

3

There are 3 answers

0
tchau.dev On

If you want to get the location path, SetupDiGetDeviceRegistryProperty is the function you're looking for. Set the property value to SPDRP_LOCATION_INFORMATION.

I'm assuming you already know how to enumerate devices to get the DeviceInfoSet and DeviceInfoData.

0
SvdSinner On

If you want to not require Windows 8, I believe WMI is the way to go:

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 didn't crack open my case to verify the SATA port numbers, but the above app looks like it gives reasonable results on my machine with 3 SATA hard drives.

0
Durgesh Pandey On
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 demo...

Note: First Add the reference of System.Management.dll of .net freamwork 4.0