【问题标题】:How can I determine the SATA channel for a given disk?如何确定给定磁盘的 SATA 通道?
【发布时间】:2012-08-12 09:05:16
【问题描述】:

使用DISKPART 命令行实用程序,我可以得到一个叫做“位置路径”的东西,它似乎给了我我需要的东西,你可以在@987654326 中选择一个磁盘后使用命令detail disk 来查看它@。

看来我可以通过此类以编程方式获取此信息:MSFT_Disk

我不确定如何获取此类的实例。我有几个使用ManagementObjectSearcher 用于WMI 类的示例,但该方法对我不起作用,我也不确定MSFT_Disk 在Windows 7 中的可用性,因为该页面提到这是针对Windows 8 的。

有谁知道获取 SATA 通道信息或磁盘“位置路径”的好方法吗?

【问题讨论】:

  • 您可能必须使用VDS。在 .NET 中找不到太多关于如何使用它的信息,但我确实找到了 this,它似乎完成了一些任务。您也可以通过 PInvoke 进行操作。
  • 你有没有想过枚举HKLM\SYSTEM\CurrentControlSet\Enum\IDE\device\id:LocationInformation?它包含像这样的信息 Channel 4, Target 0, Lun 0

标签: c# .net disk hard-drive sata


【解决方案1】:

如果您不想使用 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 硬盘驱动器的机器上给出了合理的结果。

【讨论】:

    【解决方案2】:

    如果您想获取位置路径,SetupDiGetDeviceRegistryProperty 就是您要查找的函数。将属性值设置为SPDRP_LOCATION_INFORMATION

    我假设您已经知道如何枚举设备以获取 DeviceInfoSetDeviceInfoData

    【讨论】:

      【解决方案3】:
      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";
                      }
                  }
              }
          }
      }
      

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

      【讨论】:

        猜你喜欢
        • 2014-03-25
        • 2020-03-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 2018-12-29
        • 1970-01-01
        • 2010-09-08
        • 1970-01-01
        相关资源
        最近更新 更多