Use C # to get CPU number, HDD number and other system related environment, properties

Source: Internet
Author: User

If the use of C # to obtain systems related to the environment and properties, this is also a lot of questions on the Internet, but most of the questions are not answered, recently just want to do the relevant things, sorted out, provided to you, I hope to provide you with the reference points:

First you need to define several structures (struct) to facilitate dllimport to be invoked as a return parameter. Here's the code:

CpuInfo.cs

using System;


using System.Configuration;


using System.Runtime.InteropServices;





/**//**


* Layoutkind.automatic: Allows the running state to reorder type members for increased efficiency


* Note: Never use this option to invoke a dynamically linked library function that is not governed.


* layoutkind.explicit: Sorts type members by FieldOffset property for each field


* LayoutKind.Sequential: Sorts the members of a type that appear in the domain of the jurisdictional type definition where they are not governed.


*/


/**////<summary>


///defines the information structure of the CPU


///</summary>


[StructLayout (layoutkind.sequential)]


public struct CpuInfo


... {


/**////<summary>


///OEM ID


///</summary>


public UINT Dwoemid;


/**////<summary>


///Page Size


///</summary>


public UINT dwPageSize;


public UINT lpminimumapplicationaddress;


public UINT lpmaximumapplicationaddress;


public UINT Dwactiveprocessormask;


/**////<summary>


///CPU Number


///</summary>


public UINT Dwnumberofprocessors;


/**////<summary>


///CPU Type


///</summary>


public UINT dwProcessorType;


public UINT Dwallocationgranularity;


/**////<summary>


///CPU Level


///</summary>


public UINT Dwprocessorlevel;


public UINT Dwprocessorrevision;


}


MemoryInfo.cs


using System;


using System.Configuration;


using System.Runtime.InteropServices;





/**//**


* Layoutkind.automatic: Allows the running state to reorder type members for increased efficiency


* Note: Never use this option to invoke a dynamically linked library function that is not governed.


* layoutkind.explicit: Sorts type members by FieldOffset property for each field


* LayoutKind.Sequential: Sorts the members of a type that appear in the domain of the jurisdictional type definition where they are not governed.


*/


/**////<summary>


///defines the information structure of the memory


///</summary>


[StructLayout (layoutkind.sequential)]


public struct Memoryinfo


... {


/**////<summary>


///


///</summary>


public UINT Dwlength;


/**////<summary>


///already used memory


///</summary>


public UINT dwMemoryLoad;


/**////<summary>


///Total Physical memory size


///</summary>


public UINT dwTotalPhys;


/**////<summary>


Available Physical memory size


///</summary>


public UINT dwAvailPhys;


/**////<summary>


///Exchange File Total size


///</summary>


public UINT dwTotalPageFile;


/**////<summary>


///available swap file size


///</summary>


public UINT dwAvailPageFile;


/**////<summary>


///Total virtual memory size


///</summary>


public UINT dwTotalVirtual;


/**////<summary>


///available virtual memory size


///</summary>


public UINT dwAvailVirtual;


}


SystemTimeInfo.cs


using System;


using System.Configuration;


using System.Runtime.InteropServices;





/**//**


* Layoutkind.automatic: Allows the running state to reorder type members for increased efficiency


* Note: Never use this option to invoke a dynamically linked library function that is not governed.


* layoutkind.explicit: Sorts type members by FieldOffset property for each field


* LayoutKind.Sequential: Sorts the members of a type that appear in the domain of the jurisdictional type definition where they are not governed.


*/


/**////<summary>


///The information structure that defines the system time


///</summary>


[StructLayout (layoutkind.sequential)]


public struct Systemtimeinfo


... {


/**////<summary>


///Year


///</summary>


public ushort Wyear;


/**////<summary>


///Month


///</summary>


public ushort Wmonth;


/**////<summary>


///Week


///</summary>


public ushort Wdayofweek;


/**////<summary>


///Day


///</summary>


public ushort Wday;


/**////<summary>


///hours


///</summary>


public ushort Whour;


/**////<summary>


///minutes


///</summary>


public ushort Wminute;


/**////<summary>


///seconds


///</summary>


public ushort Wsecond;


/**////<summary>


///Millisecond


///</summary>


public ushort Wmilliseconds;


}

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.