PDA

View Full Version : Lấy mã ổ cứng bằng C#



mouseover
07-05-2009, 03:25 PM
Bác nào dành API chỉ cho em cách lấy mã của ổ cứng với. Nếu mà có code thì càng tuyệt vì em cũng gà lắm. Giúp đỡ em nhé em đang máu mà. Gà khổ thế đấy):)T

Xcross87
07-05-2009, 04:05 PM
Mã của ổ cứng là cái gì thế?

dieucay555
07-05-2009, 04:09 PM
Hi,ko rõ là bạn cần lấy mã gì thật. Còn nếu muốn xác định thông tin phần cứng máy tính có thể sử dụng namespace System.Management;


using System;
using System.Text;
using System.Management;

namespace PC
{
public class WMI
{
/// <summary>
/// method to retrieve the selected HDD's serial number
/// </summary>
/// <param name="strDriveLetter">Drive letter to retrieve serial number for</param>
/// <returns>the HDD's serial number</returns>
public string GetHDDSerialNumber(string drive)
{
//check to see if the user provided a drive letter
//if not default it to "C"
if (drive == "" || drive == null)
{
drive = "C";
}
//create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\"");
//bind our management object
disk.Get();
//return the serial number
return disk["VolumeSerialNumber"].ToString();
}

/// <summary>
/// method to retrieve the HDD's freespace
/// </summary>
/// <param name="drive">Drive letter to get free space from (optional)</param>
/// <returns>The free space of the selected HDD</returns>
public double GetHDDFreeSpace(string drive)
{
//check to see if the user provided a drive letter
//if not default it to "C"
if (drive == "" || drive == null)
{
drive = "C";
}
//create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\"");
//bind our management object
disk.Get();
//return the free space amount
return Convert.ToDouble(disk["FreeSpace"]);
}

/// <summary>
/// method to retrieve the HDD's size
/// </summary>
/// <param name="drive">Drive letter to get free space from (optional)</param>
/// <returns>The free space of the selected HDD</returns>
public double getHDDSize(string drive)
{
//check to see if the user provided a drive letter
//if not default it to "C"
if (drive == "" || drive == null)
{
drive = "C";
}
//create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\"");
//bind our management object
disk.Get();
//return the HDD's initial size
return Convert.ToDouble(disk["Size"]);
}

/// <summary>
/// method to find out the IPEnabled of
/// the network adapter
/// </summary>
/// <returns></returns>
public bool IsIPEnabled()
{
//create out management class object using the
//Win32_NetworkAdapterConfiguration class to get the attributes
//of the network adapter
ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
//create our ManagementObjectCollection to get the attributes with
ManagementObjectCollection objCol = mgmt.GetInstances();
bool enabled = false;
foreach (ManagementObject obj in objCol)
{
//get the status of the first adapter then
//exit the loop
enabled = (bool)obj["IPEnabled"];
break;
//dispose of the object
obj.Dispose();
}
//return the IPEnabled status
return enabled;
}

/// <summary>
/// Returns MAC Address from first Network Card in Computer
/// </summary>
/// <returns>MAC Address in string format</returns>
public string FindMACAddress()
{
//create out management class object using the
//Win32_NetworkAdapterConfiguration class to get the attributes
//of the network adapter
ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
//create our ManagementObjectCollection to get the attributes with
ManagementObjectCollection objCol = mgmt.GetInstances();
string address = String.Empty;
//loop through all the objects we find
foreach (ManagementObject obj in objCol)
{
if (address == String.Empty) // only return MAC Address from first card
{
//grab the value from the first network adapter we find
//you can change the string to an array and get all
//network adapters found as well
//check to see if the adapter's IPEnabled
//equals true
if ((bool)obj["IPEnabled"] == true)
{
address = obj["MacAddress"].ToString();
}
}
//dispose of our object
obj.Dispose();
}
//replace the ":" with an empty space, this could also
//be removed if you wish
address = address.Replace(":", "");
//return the mac address
return address;
}

/// <summary>
/// method to retrieve the network adapters
/// default IP gateway using WMI
/// </summary>
/// <returns>adapters default IP gateway</returns>
public string GetDefaultIPGateway()
{
//create out management class object using the
//Win32_NetworkAdapterConfiguration class to get the attributes
//of the network adapter
ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
//create our ManagementObjectCollection to get the attributes with
ManagementObjectCollection objCol = mgmt.GetInstances();
string gateway = String.Empty;
//loop through all the objects we find
foreach (ManagementObject obj in objCol)
{
if (gateway == String.Empty) // only return MAC Address from first card
{
//grab the value from the first network adapter we find
//you can change the string to an array and get all
//network adapters found as well
//check to see if the adapter's IPEnabled
//equals true
if ((bool)obj["IPEnabled"] == true)
{
gateway = obj["DefaultIPGateway"].ToString();
}
}
//dispose of our object
obj.Dispose();
}
//replace the ":" with an empty space, this could also
//be removed if you wish
gateway = gateway.Replace(":", "");
//return the mac address
return gateway;
}

/// <summary>
/// Return processorId from first CPU in machine
/// </summary>
/// <returns>[string] ProcessorId</returns>
public string GetCPUId()
{
string cpuInfo = String.Empty;
//create an instance of the Managemnet class with the
//Win32_Processor class
ManagementClass mgmt = new ManagementClass("Win32_Processor");
//create a ManagementObjectCollection to loop through
ManagementObjectCollection objCol = mgmt.GetInstances();
//start our loop for all processors found
foreach(ManagementObject obj in objCol)
{
if(cpuInfo == String.Empty)
{
// only return cpuInfo from first CPU
cpuInfo = obj.Properties["ProcessorId"].Value.ToString();
}
}
return cpuInfo;
}

/// <summary>
/// method for retrieving the CPU Manufacturer
/// using the WMI class
/// </summary>
/// <returns>CPU Manufacturer</returns>
public string GetCPUManufacturer()
{
string cpuMan = String.Empty;
//create an instance of the Managemnet class with the
//Win32_Processor class
ManagementClass mgmt = new ManagementClass("Win32_Processor");
//create a ManagementObjectCollection to loop through
ManagementObjectCollection objCol = mgmt.GetInstances();
//start our loop for all processors found
foreach (ManagementObject obj in objCol)
{
if (cpuMan == String.Empty)
{
// only return manufacturer from first CPU
cpuMan = obj.Properties["Manufacturer"].Value.ToString();
}
}
return cpuMan;
}

/// <summary>
/// method for retrieving the CPU status
/// using the WMI class
/// </summary>
/// <returns>the CPU status</returns>
public string GetCPUStatus()
{
string cpuStatus = String.Empty;
//create an instance of the Managemnet class with the
//Win32_Processor class
ManagementClass mgmt = new ManagementClass("Win32_Processor");
//create a ManagementObjectCollection to loop through
ManagementObjectCollection objCol = mgmt.GetInstances();
//start our loop for all processors found
foreach (ManagementObject obj in objCol)
{
if (cpuStatus == String.Empty)
{
// only return cpuStatus from first CPU
cpuStatus = obj.Properties["Status"].Value.ToString();
}
}
//return the status
return cpuStatus;
}

/// <summary>
/// method to retrieve the CPU's current
/// clock speed using the WMI class
/// </summary>
/// <returns>Clock speed</returns>
public int GetCPUCurrentClockSpeed()
{
int cpuClockSpeed = 0;
//create an instance of the Managemnet class with the
//Win32_Processor class
ManagementClass mgmt = new ManagementClass("Win32_Processor");
//create a ManagementObjectCollection to loop through
ManagementObjectCollection objCol = mgmt.GetInstances();
//start our loop for all processors found
foreach (ManagementObject obj in objCol)
{
if (cpuClockSpeed == 0)
{
// only return cpuStatus from first CPU
cpuClockSpeed = Convert.ToInt32(obj.Properties["CurrentClockSpeed"].Value.ToString());
}
}
//return the status
return cpuClockSpeed;
}
}
}

mouseover
07-05-2009, 09:23 PM
Mã ổ cứng như kiểu là Serial ấy, mỗi một cái ổ cứng chỉ có một mã thôi. Bài viết của bác Mod chi tiết quá nhưng mà chưa kip đọc Thanks cái đã(Y:DY)
Mà bác Mod ơi cho em hỏi thêm một chút cái GetCPUId ấy nó là cái gì thế, đọc mã của chip à, hay là công dụng gì vậy ta

mouseover
07-05-2009, 09:49 PM
Oái nó báo thế này. Do đâu vậy bộ cài hay là cái gì vậy các bác ?

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Giờ mà khắc phục thì làm thế nào đây, có phải là do bản .net ko, hay thử up lên 3,5 sp1 nhỉ xem sao đã

Xcross87
07-05-2009, 09:58 PM
Add Reference => system.management.dll

không thì lấy serial của HDD qua API



[DllImport("kernel32.dll")]
private static extern long GetVolumeInformation(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize);

public string GetVolumeSerial(string strDriveLetter)
{
uint serNum = 0;
uint maxCompLen = 0;
StringBuilder VolLabel = new StringBuilder(256); // Label
UInt32 VolFlags = new UInt32();
StringBuilder FSName = new StringBuilder(256); // File System Name
strDriveLetter+=":\\"; // fix up the passed-in drive letter for the API call
long Ret = GetVolumeInformation(strDriveLetter, VolLabel, (UInt32)VolLabel.Capacity, ref serNum, ref maxCompLen, ref VolFlags, FSName, (UInt32)FSName.Capacity);

return Convert.ToString(serNum);
}

mouseover
07-05-2009, 10:03 PM
hic siêu quá. Khen một câu mà cũng ko được khen ngắn hic hic