اندازهی قلم متن
تخمین مدت زمان مطالعهی مطلب:
یک دقیقه
شما در حال نوشتن یک نرم افزار هستید و برای این نرم افزار ممکن است ماهها وقت صرف کرده باشید؛ پس باید به دنبال راهی باشید که بتوانید از آن محافظت کنید. راههای متعددی برای Trial کردن نرم افزار وجود دارند که یکی از این راهها استفاده از سریال سخت افزارهای کامپیوتر کاربر است. همانطور که میدانید هر سخت افزار یک شمارهی سریال مخصوص خودش را دارد و بدین طریق میتوان یک شماره سریال منحصر به فرد را تولید کرد. ما در این مقاله برای بدست آوردن کلیهی مشخصات سخت افزار یک کامپیوتر از کلاس ManagementObjectSearcher در فضای نام System.Management استفاده کردهایم.
using System.Management; using System.Windows.Forms; namespace HardwareSerialNumber { class Program { static void Main() { string serialNumber = string.Empty; ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor"); foreach (var o in searcher.Get()) { var query = (ManagementObject)o; serialNumber = serialNumber + query["ProcessorId"]; } MessageBox.Show(string.Format("Processor Serial Number :{0}", serialNumber),"Serial Number",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); } } }
خوب. حالا به بررسی کدهای بالا میپردازیم. در خط 10 یک شیء از کلاس MangementObjectSearcher ایجاد کردهایم . سازندهی این کلاس 2 پارامتر را میپذیرد. اولین پارامتر یک رشته میباشد CIMV2 . root\\CIMV2 فضای نام پیش فرض مخصوص کوئریهای WMI میباشد و پارامتر دوم کوئری WMI است. عبارت Win32_Processor اشاره به کلاس Win32_processor دارد. این کلاس بیانگر یک کلاس مدیریتی از نوع CIM (مراجعه کنید به اینجا و اینجا ) است. در خط 11 با استفاده از متد Get، تمامی اطلاعات مربوط به Processor را بارگذاری میکنیم و سپس فیلد ProcessorId را در یک رشته قرار میدهیم. نکتهی دیگر اینکه ارجاعی را به فایل System.Managment.Dll به پروژهی خود اضافه کنید.
نتیجهی کد بالا :
در این آدرس یک کتابخانه که شامل تمامی مثالها میباشد قابل در یافت است.