从远程SQL Server安装的硬盘中查找硬盘序列号

时间:2012-07-16 08:54:00

标签: c#

我有一个带有SQL Server的C#.net Windows表单应用程序。我的应用程序在本地网络的多用户中使用。现在我需要找到硬盘安装sql server的硬盘序列号(注意:使用C#.net应用程序数据源)。

如何找到按应用程序数据源抛出的硬盘序列号?

3 个答案:

答案 0 :(得分:0)

您需要诉诸WMI。使用SQL Server计算机上的适当权限,您可以获取其上的硬盘驱动器的序列号。有关使用WMI检索硬盘序列号的示例,请参阅here

您需要确定哪个磁盘容纳SQL Server以及如何自己访问SQL Server计算机。

答案 1 :(得分:0)

这可能很有用

using System;
using System.Management;
using System.Collections;

namespace WmiControl
{
    public class WMI
    {
        public bool GetDiskSerial(string Computername)
        {

            try
            {
                ManagementScope scope = new ManagementScope(@"\\" + Computername + @"\root\cimv2");
                scope.Connect();
                ArrayList hdCollection;
                ManagementObjectSearcher searcher;
                if (GetDiskDrive(scope, out hdCollection, out searcher) || GetDiskSerial(scope, hdCollection, ref searcher))
                    return true;
                else 
                    return false;
            }
            catch (ManagementException)
            {
                return false;
            }

        }

        private bool GetDiskSerial(ManagementScope scope, ArrayList hdCollection, ref ManagementObjectSearcher searcher)
        {
            try
            {


                ObjectQuery query1 = new ObjectQuery("SELECT * FROM Win32_PhysicalMedia");

                searcher = new ManagementObjectSearcher(scope, query1);
                int i = 0;
                string sDiskSerial = "";
                foreach (ManagementObject wmi_HD in searcher.Get())
                {
                    // get the hard drive from collection
                    // using index
                    if (i < hdCollection.Count)
                    {
                        HardDrive hd = (HardDrive)hdCollection[i];
                        if (wmi_HD["SerialNumber"] == null)
                            hd.SerialNo = "";
                        else
                            hd.SerialNo = wmi_HD["SerialNumber"].ToString();
                    }
                    ++i;
                }
                foreach (HardDrive hd in hdCollection)
                {
                    if (!String.IsNullOrEmpty(hd.SerialNo))
                    {
                        sDiskSerial = hd.SerialNo;
                        break;
                    }
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private bool GetDiskDrive(ManagementScope scope, out ArrayList hdCollection, out ManagementObjectSearcher searcher)
        {
            try
            {
                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_DiskDrive");
                hdCollection = new ArrayList();
                searcher = new ManagementObjectSearcher(scope, query);
                foreach (ManagementObject wmi_HD in searcher.Get())
                {
                    HardDrive hd = new HardDrive();
                    hd.Model = wmi_HD["Model"].ToString();
                    hd.Type = wmi_HD["InterfaceType"].ToString();
                    hdCollection.Add(hd);
                    return true;
                }
                return true;
            }
            catch (Exception)
            {
                hdCollection = null;
                searcher = null;
                return false;
            }
        }
    }
    class HardDrive
    {
        private string model = null;
        private string type = null;
        private string serialNo = null;
        public string Model
        {
            get { return model; }
            set { model = value; }
        }
        public string Type
        {
            get { return type; }
            set { type = value; }
        }
        public string SerialNo
        {
            get { return serialNo; }
            set { serialNo = value; }
        }
    }
}

See here for more info

You might want also to study CLR

答案 2 :(得分:0)

你必须分3个阶段完成:

  • 数据源的连接字符串为您提供安装SQL Server的计算机的名称
  • 然后,您必须查询计算机以找出
  • 上安装了哪个驱动器SQL Server的驱动器
  • 然后,您可以使用此处其他人提供的代码来获取序列号