Computer Related operation API

Source: Internet
Author: User

 Author: Liu Qichao, Rockay. Liu

Email: Rockay_liu@126.com

 

Using System;
Using System. Management;
Using System. IO;
Using System. Runtime. InteropServices;
Using System. Security. Cryptography;
Using System. Text;
Using Microsoft. Win32;

Namespace PC
{
Class PCTool
{
/// <Summary>
/// Obtain the cpu serial number
/// </Summary>
/// <Returns> string </returns>
Public static string GetCpuInfo ()
{
String cpuInfo = "";
ManagementClass cimobject = new ManagementClass ("Win32_Processor ");
ManagementObjectCollection moc = cimobject. GetInstances ();
Foreach (ManagementObject mo in moc)
{
CpuInfo = mo. Properties ["ProcessorId"]. Value. ToString ();
}
Return cpuInfo. ToString ();
}

/// <Summary>
/// Obtain the hard disk ID
/// </Summary>
/// <Returns> string </returns>
Public static string GetHDid ()
{
String HDid = "";
ManagementClass cimobject1 = new ManagementClass ("Win32_DiskDrive ");
ManagementObjectCollection moc1 = cimobject1.GetInstances ();
Foreach (ManagementObject mo in moc1)
{
HDid = (string) mo. Properties ["Model"]. Value;
}
Return HDid. ToString ();
}

/// <Summary>
/// Obtain the Mac address of the NIC
/// </Summary>
/// <Returns> </returns>
Public static string GetMacAddress ()
{
System. Management. ManagementObjectSearcher mos =
New ManagementObjectSearcher ("SELECT * FROM Win32_NetworkAdapterConfiguration ");

Foreach (ManagementObject m in mos. Get ())
{
If (bool) m ["IPEnabled"])
{
Return m ["MACAddress"]. ToString ();
}
}
Return "";
}

/// <Summary>
/// Obtain the IP address
/// </Summary>
/// <Returns> </returns>
Public static string GetIPAddress ()
{
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;
}
}
Return st;
}

/// <Summary>
/// Obtain the user name
/// </Summary>
/// <Returns> </returns>
Public static 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>
/// Obtain the system type
/// </Summary>
/// <Returns> </returns>
Public static 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>
Public static 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>
/// Obtain the machine name
/// </Summary>
/// <Returns> </returns>
Public static string GetComputerName ()
{
Try
{
Return System. Environment. GetEnvironmentVariable ("ComputerName ");
}
Catch
{
Return "unknow ";
}
Finally
{
}

}

# Region Shutdown

[StructLayout (LayoutKind. Sequential, Pack = 1)]

Internal struct TokPriv1Luid
{
Public int Count;

Public long Luid;

Public int Attr;
}

[DllImport ("kernel32.dll", ExactSpelling = true)]

Internal static extern IntPtr GetCurrentProcess ();

[DllImport ("advapi32.dll", ExactSpelling = true, SetLastError = true)]

Internal static extern bool OpenProcessToken (IntPtr h, int acc, ref IntPtr phtok );

[DllImport ("advapi32.dll", SetLastError = true)]

Internal static extern bool LookupPrivilegeValue (string host, string name, ref long pluid );

[DllImport ("advapi32.dll", ExactSpelling = true, SetLastError = true)]

Internal static extern bool AdjustTokenPrivileges (IntPtr htok, bool disall,

Ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );

[DllImport ("user32.dll", ExactSpelling = true, SetLastError = true)]

Internal static extern bool ExitWindowsEx (int flg, int rea );

Internal const int SE_PRIVILEGE_ENABLED = 0x00000002;

Internal const int TOKEN_QUERY = 0x00000008;

Internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

Internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege ";

Internal const int EWX_LOGOFF = 0x00000000;

Internal const int EWX_SHUTDOWN = 0x00000001;

Internal const int EWX_REBOOT = 0x00000002;

Integer const int EWX_FORCE = 0x00000004;

Internal const int EWX_POWEROFF = 0x00000008;
/// <Summary>
/// Shutdown command
/// </Summary>
/// <Param name = "flg"> parameters </param>
Private static void DoExitWin (int flg)
{
Bool OK;

TokPriv1Luid tp;

IntPtr hproc = GetCurrentProcess ();

IntPtr htok = IntPtr. Zero;

OK = OpenProcessToken (hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );

Tp. Count = 1;

Tp. Luid = 0;

Tp. Attr = SE_PRIVILEGE_ENABLED;

OK = LookupPrivilegeValue (null, SE_SHUTDOWN_NAME, ref tp. Luid );

OK = AdjustTokenPrivileges (htok, false, ref tp, 0, IntPtr. Zero, IntPtr. Zero );

OK = ExitWindowsEx (flg, 0 );
}

/// <Summary>
/// Shutdown
/// </Summary>
Public static void ShutdownPC ()
{
DoExitWin (EWX_SHUTDOWN );

}

/// <Summary>
/// Cancel
/// </Summary>
Public static void LogOff ()
{
DoExitWin (EWX_LOGOFF );

}

/// <Summary>
/// Restart
/// </Summary>
Public static void ReBoot ()
{
DoExitWin (EWX_REBOOT );

}

/// <Summary>
/// Shutdown
/// </Summary>
Public static void PowerOff ()
{
DoExitWin (EWX_POWEROFF );

}

/// <Summary>
/// Force Shutdown
/// </Summary>
Public static void Force ()
{
DoExitWin (EWX_FORCE );

}

# Endregion

}
}

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.