获取GUID-HDD和GUID-Processor C# [英] Get GUID-HDD and GUID-Processor C#

查看:99
本文介绍了获取GUID-HDD和GUID-Processor C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我需要从我的电脑上获取GUID-HDD和GUID-Processor,我怎样才能用C#获得它?我正在努力获得处理器GUID:

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Management;

命名空间 SerialHDD
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher( SELECT * FROM Win32_Processor);

foreach (ManagementObject proc in searcher.Get())
{
label1.Text = proc [ UniqueId]。ToString();
}
}
}
}

这段代码给了我NulReferenseExeption他找不到UniqueId的值我从 http://msdn.microsoft.com/en-us/library/aa394373(ⅴ = vs.85).aspx [ ^ ]

请帮帮我。如果sombody知道获取GUID的其他方式,请在此处编写代码。提前谢谢))。

解决方案

我也有相同的结果。您只需要取消引用属性值(通过在其实例中调用方法 ToString 来实现),因为属性的值可以合法地为null。至少只检查一下。



你的方法不太正确。管理文档很好,但应该通过运行时为您提供确认。 ManagementObject 提供了一些反射接口:它告诉您可用的属性。使用 System.Management.ManagementObject.Properties

http://msdn.microsoft.com/en-us/library/system.management.managementbaseobject.properties.aspx [ ^ ]。



通过这种方式,您可以了解可用的属性并找到所有属性。例如:

 ManagementObjectSearcher searcher =  new  ManagementObjectSearcher(   SELECT * FROM Win32_Processor); 
ManagementObjectCollection items = searcher.Get();
foreach (ManagementObject item in items){
foreach (PropertyData property in item.Properties)
Console.WriteLine( {0}:{1},property.Name,item [property.Name]);
} // loop





输出它以查看您拥有的产品。唯一ID为null,是的,但您会看到可以使用ProcessorId,Name,...您可以使用其他系统组件进行识别,例如HD唯一ID ...



-SA


如果您尝试使用它来唯一地识别机器以获得某种安全性,那么您运气不好。它不会工作。



首先,英特尔是唯一一个实施CPUID序列号的人。它在AMD处理器上并不存在。此外,在大多数基于Intel的机器上,CPUID序列号默认关闭,所以没有价值!



此外,英特尔不是再将序列号放入处理器中。所以,完全有可能你的代码是正确的,没有要报告的序列号!


你好,

你将获得使用此号码的硬盘号码

 ManagementClass mangnmt =  new  ManagementClass(  Win32_LogicalDisk); 
ManagementObjectCollection mcol = mangnmt.GetInstances();
string result = ;
foreach (ManagementObject strt in mcol)
{
result + = Convert.ToString(strt [ VolumeSerialNumber]);
}
lblHddSrNo.Text =结果;





您将使用此处获得ProcessorID

 ManagementClass mc =  new  ManagementClass(  win32_processor); 
ManagementObjectCollection moc = mc.GetInstances();
字符串 Id = 字符串 .Empty;
foreach (ManagementObject mo in moc)
{

Id = mo.Properties [ processorID]。Value.ToString();
break ;
}

lblProcessorId.Text = Id;





必须包括

 使用 System.Management; 

表示这些。

可能它对你有帮助......


Hi I need to get GUID-HDD and GUID-Processor from my PC, how can I get that with C#?? I''m trying with to get Processor GUID:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;

namespace SerialHDD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ManagementObjectSearcher searcher = new
            ManagementObjectSearcher("SELECT * FROM Win32_Processor");

            foreach (ManagementObject proc in searcher.Get())
            {
                label1.Text = proc["UniqueId"].ToString();
            }
        }
    }
}

This code gives me NulReferenseExeption he can''t find value of "UniqueId" I find this parameter from http://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx[^]
Please help me. If sombody knows other way to get GUIDs please write code here.Thanks in advance)).

解决方案

I have the same result, too. You just don''t have to dereference property value (you do it by calling the method ToString in its instance), because the value of the property can be null, legitimately. At least just check for null.

Your approach is not quite correct. Management documentation is good, but it should be confirmed with what runtime gives you. ManagementObject provides some reflective interface: it tells you what properties are available. Use System.Management.ManagementObject.Properties:
http://msdn.microsoft.com/en-us/library/system.management.managementbaseobject.properties.aspx[^].

This way, you can learn what are the available properties and find them all. For example:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
ManagementObjectCollection items = searcher.Get();
foreach (ManagementObject item in items) {
    foreach (PropertyData property in item.Properties)
        Console.WriteLine("{0}: {1}", property.Name, item[property.Name]);
} //loop



Output it to see what you have. Unique ID is null, yes, but you will see that you can use "ProcessorId", "Name", … You can use other system components for identification, such as HD unique ID…

—SA


If you''re trying to use this to uniquely identify a machine for some security, you''re out of luck. It won''t work.

First, Intel was the only one to implement the CPUID Serial Number. It doesn''t exist on AMD processors. Also, on most Intel-based machines, the CPUID Serial Number is turned OFF by default, so there isn''t a value to get!

Also, Intel is not putting the serial number into processors any more. So, it''s entirely possible your code is correct, there is no serial number to report!


Hello,
You will get HDD number using this

ManagementClass mangnmt = new ManagementClass("Win32_LogicalDisk");
            ManagementObjectCollection mcol = mangnmt.GetInstances();
            string result = "";
            foreach (ManagementObject strt in mcol)
            {
                result += Convert.ToString(strt["VolumeSerialNumber"]);
            }
            lblHddSrNo.Text = result;



You will get ProcessorID using this

ManagementClass mc = new ManagementClass("win32_processor");
            ManagementObjectCollection moc = mc.GetInstances();
            String Id = String.Empty;
            foreach (ManagementObject mo in moc)
            {

                Id = mo.Properties["processorID"].Value.ToString();
                break;
            }

            lblProcessorId.Text = Id;



Must include

using System.Management;

for these.
May it''s helps you...


这篇关于获取GUID-HDD和GUID-Processor C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆