7

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?

Jon
  • 1,379
  • 1
  • 12
  • 32
  • You'll probably have to use [VDS](http://msdn.microsoft.com/en-us/library/windows/desktop/bb986750%28v=vs.85%29.aspx). Can't find much on how to using it from .NET but I did find [this](http://esotericalstuff.wordpress.com/2010/11/18/vds-activities-under-c-2/) which seems to go through doing some tasks. You may be able to PInvoke your way through it as well. – Jeff Mercado Aug 16 '12 at 03:05
  • Have you thought about enumerating `HKLM\SYSTEM\CurrentControlSet\Enum\IDE\device\id:LocationInformation`? It contains information like this **Channel 4, Target 0, Lun 0** – vane Aug 31 '12 at 17:27

3 Answers3

0

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.

dotjoe
  • 26,242
  • 5
  • 63
  • 77
SvdSinner
  • 951
  • 1
  • 11
  • 23
0

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.

tchau.dev
  • 903
  • 1
  • 11
  • 30
0
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

Durgesh Pandey
  • 2,314
  • 4
  • 29
  • 43