Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

c# - Safest way to get processor ID or some hardware info

I need to get any info to identify a machine and what I was using so far was the following:

    internal static string GetProcessorId()
    {
        try
        {
            StringBuilder sb = new StringBuilder();
            using (System.Management.ManagementClass theClass = new System.Management.ManagementClass("Win32_Processor"))
            {
                using (System.Management.ManagementObjectCollection theCollectionOfResults = theClass.GetInstances())
                {
                    foreach (System.Management.ManagementObject currentResult in theCollectionOfResults)
                    {
                        sb.Append(currentResult["ProcessorID"].ToString());
                    }
                }
            }
            return sb.ToString();
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
            return "";
        }
    }

But when I run this on Windows XP (from a Virtual Machine), my currentResult["ProcessorID"] is null. I'm not sure if it is because I'm using a virtual machine, but it might be because is XP. Anyway, I'm not very trustful of this code. Is it safe enough? Should I get a computer Id some other way? This is all part of a licensing system I'm developing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For a small tool I wrote (Windows Forms .NET 2.0), I needed something similar.

I wrote a helper class that simply uses some common information like HDD serial number through PInvoke of the GetVolumeInformation function.

This is by no way really safe or bullet-proof, but accurate enough to fit my needs.

(If you are interested this is the tool, if I'm allowed to link to)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...