如何确定给定磁盘的SATA通道?

7
使用DISKPART命令行实用程序,我可以获取称为“位置路径”的内容,该内容似乎可以提供我所需的信息,您可以在选择其中一个磁盘后使用diskpart中的命令detail disk查看此内容。
通过这个类,似乎可以以编程方式获取此信息:MSFT_Disk 我不确定如何获取此类的实例。我有一些使用ManagementObjectSearcher来搜索WMI类的示例,但该方法对我不起作用,而且我也不确定MSFT_Disk在Windows 7中是否可用,因为该页面提到这是针对Windows 8的。
是否有人知道获取SATA通道信息或磁盘的“位置路径”的好方法?

你可能需要使用 VDS。虽然我没有找到太多关于如何在.NET中使用它的信息,但我找到了 这个链接,其中似乎介绍了一些任务的处理过程。您也可以尝试使用 PInvoke 来进行操作。 - Jeff Mercado
你有没有想过枚举HKLM\SYSTEM\CurrentControlSet\Enum\IDE\device\id:LocationInformation?它包含像通道4,目标0,Lun 0这样的信息。 - vane
3个回答

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...

注意:首先添加 .net framework 4.0 的 System.Management.dll 引用。

0
如果你想不依赖 Windows 8,我认为 WMI 是一个好选择:
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();
        }
    }
}

我没有打开机箱验证SATA端口号,但上面的应用程序看起来在我的3个SATA硬盘的机器上给出了合理的结果。


0
如果您想获取位置路径,SetupDiGetDeviceRegistryProperty 是您要查找的函数。将属性值设置为 SPDRP_LOCATION_INFORMATION
我假设您已经知道如何枚举设备以获取 DeviceInfoSetDeviceInfoData

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接