Obtain information about the local machine in Asp.net.

Source: Internet
Author: User

Note: first, add reference system. Management in the project bin directory.

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Management;

/// <Summary>
/// Computer Summary
/// </Summary>

Public class computer
{
Public String cpuid; // CPU ID
Public int cpucount; // Number of CPUs
Public String [] cpumhz; // CPU frequency unit: Hz
Public String macaddress; // the MAC address of the computer
Public String diskid; // hard disk ID
Public String disksize; // hard disk size unit: bytes
Public String IPaddress; // the IP address of the computer.
Public String loginusername; // operating system logon Username
Public String computername; // computer name
Public String systemtype; // system type
Public String totalphysicalmemory; // total memory unit: m
Private Static computer _ instance;
Public static computer instance ()
{
If (_ instance = NULL)
_ Instance = new computer ();
Return _ instance;
}
Public Computer ()
{
Cpuid = getcpuid ();
Cpucount = getcpucount ();
Cpumhz = getcpumhz ();
Macaddress = getmacaddress ();
Diskid = getdiskid ();
Disksize = getsizeofdisk ();
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
{
}

}
Public static int getcpucount ()
{
Try
{
Using (managementclass mcpu = new managementclass ("win32_processor "))
{
Managementobjectcollection CPUs = mcpu. getinstances ();
Return CPUs. count;
}
}
Catch
{
}
Return-1;
}
Public static string [] getcpumhz ()
{
Managementclass MC = new managementclass ("win32_processor ");
Managementobjectcollection CPUs = mc. getinstances ();

String [] MHz = new string [CPUs. Count];
Int C = 0;
Managementobjectsearcher MySearch = new managementobjectsearcher ("select * From win32_processor ");
Foreach (managementobject Mo in MySearch. Get ())
{
MHz [c] = Mo. properties ["currentclockspeed"]. value. tostring ();
C ++;
}
MC. Dispose ();
MySearch. Dispose ();
Return MHz;
}
Public static string getsizeofdisk ()
{
Managementclass MC = new managementclass ("win32_diskdrive ");
Managementobjectcollection MOJ = mc. getinstances ();
Foreach (managementobject m in MOJ)
{
Return M. properties ["size"]. value. tostring ();
}
Return "-1 ";
}
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
{
}

}< BR >///


// username for logging on to the Operating System
///
///
string GetUserName ()
{< br> try
{< br> 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
{
}

}
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
{
}

}
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
{
}
}
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.