C # obtain hardware information (2)

Source: Internet
Author: User

Using system;
Using system. Management;
Namespace soyee. Comm
{
/// <Summary>
/// Computer Information
/// </Summary>
Public class computer
{
Public String cpuid;
Public String macaddress;
Public String diskid;
Public String IPaddress;
Public String loginusername;
Public String computername;
Public String systemtype;
Public String totalphysicalmemory; // unit: m
Private Static computer _ instance;
Public static computer instance ()
{
If (_ instance = NULL)
_ Instance = new computer ();
Return _ instance;
}
Protected computer ()
{
Cpuid = getcpuid ();
Macaddress = getmacaddress ();
Diskid = getdiskid ();
IPaddress = getipaddress ();
Loginusername = GetUserName ();
Systemtype = getsystemtype ();
Totalphysicalmemory = gettotalphysicalmemory ();
Computername = getcomputername ();
}
String getcpuid ()
{
Try
{
// Obtain the CPU serial number code
String cpuinfo = ""; // CPU serial number
Managementclass MC = new managementclass ("win32_processor ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
Cpuinfo = Mo. properties ["processorid"]. value. tostring ();
}
MoC = NULL;
MC = NULL;
Return cpuinfo;
}
Catch
{
Return "unknow ";
}
Finally
{
}

}
String getmacaddress ()
{
Try
{
// Obtain the hardware address of the NIC
String MAC = "";
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (bool) Mo ["ipenabled"] = true)
{
MAC = Mo ["macaddress"]. tostring ();
Break;
}
}
MoC = NULL;
MC = NULL;
Return MAC;
}
Catch
{
Return "unknow ";
}
Finally
{
}

}
String getipaddress ()
{
Try
{
// Obtain the IP address
String ST = "";
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (bool) Mo ["ipenabled"] = true)
{
// St = Mo ["IPaddress"]. tostring ();
System. array Ar;
AR = (system. array) (Mo. properties ["IPaddress"]. value );
St = ar. getvalue (0). tostring ();
Break;
}
}
MoC = NULL;
MC = NULL;
Return st;
}
Catch
{
Return "unknow ";
}
Finally
{
}

}

String getdiskid ()
{
Try
{
// Obtain the hard disk ID
String Hdid = "";
Managementclass MC = new managementclass ("win32_diskdrive ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
Hdid = (string) Mo. properties ["model"]. value;
}
MoC = NULL;
MC = NULL;
Return Hdid;
}
Catch
{
Return "unknow ";
}
Finally
{
}

}

/// <Summary>
/// Username used to log on to the Operating System
/// </Summary>
/// <Returns> </returns>
String GetUserName ()
{
Try
{
String ST = "";
Managementclass MC = new managementclass ("win32_computersystem ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{

St = Mo ["username"]. tostring ();

}
MoC = NULL;
MC = NULL;
Return st;
}
Catch
{
Return "unknow ";
}
Finally
{
}

}

/// <Summary>
/// Pc type
/// </Summary>
/// <Returns> </returns>
String getsystemtype ()
{
Try
{
String ST = "";
Managementclass MC = new managementclass ("win32_computersystem ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{

St = Mo ["systemtype"]. tostring ();

}
MoC = NULL;
MC = NULL;
Return st;
}
Catch
{
Return "unknow ";
}
Finally
{
}

}

/// <Summary>
/// Physical memory
/// </Summary>
/// <Returns> </returns>
String gettotalphysicalmemory ()
{
Try
{

String ST = "";
Managementclass MC = new managementclass ("win32_computersystem ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{

St = Mo ["totalphysicalmemory"]. tostring ();

}
MoC = NULL;
MC = NULL;
Return st;
}
Catch
{
Return "unknow ";
}
Finally
{
}
}
/// <Summary>
///
/// </Summary>
/// <Returns> </returns>
String getcomputername ()
{
Try
{
Return System. environment. getenvironmentvariable ("computername ");
}
Catch
{
Return "unknow ";
}
Finally
{
}
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.