Super powerful computer information retrieval (CPU serial number, NIC address, IP address, hard disk ID, logon system user, computer name, etc. can be obtained)

Source: Internet
Author: User

Using system;
Using system. Management;
Namespace zgx. Common
{
/// <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;
}
/// <Summary>
/// Constructor
/// </Summary>
Public Computer ()
{
Cpuid = getcpuid ();
Macaddress = getmacaddress ();
Diskid = getdiskid ();
IPaddress = getipaddress ();
Loginusername = GetUserName ();
Systemtype = getsystemtype ();
Totalphysicalmemory = gettotalphysicalmemory ();
Computername = getcomputername ();
}
/// <Summary>
/// Obtain the CPU serial number
/// </Summary>
/// <Returns> </returns>
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
{
}

}
/// <Summary>
/// Obtain the hardware address of the NIC
/// </Summary>
/// <Returns> </returns>
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
{
}

}
/// <Summary>
/// Obtain the IP address
/// </Summary>
/// <Returns> </returns>
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
{
}

}
/// <Summary>
/// Obtain the hard disk ID
/// </Summary>
/// <Returns> </returns>
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
{
}

}

///


// physical memory
///
///
string gettotalphysicalmemory ()
{< br> 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>
/// Computer name
/// </Summary>
/// <Returns> </returns>
String getcomputername ()
{
Try
{
Return System. environment. getenvironmentvariable ("computername ");
}
Catch
{
Return "unknow ";
}
Finally
{
}
}

}
}

Related Article

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.