Using C # to obtain system information

Source: Internet
Author: User
System information is not external to the system hardware and software information, need to detect the information is also this information.. NET class libraries provide us with some of the functions needed to obtain system messages, which has brought a lot of convenience to our program design.



The following sections describe the use of the most basic three class libraries in these class libraries.



SystemInformation class



In these class libraries, the most basic class library is the SystemInformation class, contained in the System.Windows.Forms namespace. This class library can help us get basic information about the system. This class contains only a few public methods inherited from the System.Object class, and no other method is provided. The information of the system can be obtained through the public static members of this class, and we can select the appropriate members based on the information we need to obtain. Here's an example of how to use these members:



The SystemInformation class is in the System.Windows.Forms namespace and needs to be introduced before it can be used;

Using System.Windows.Forms;

Using static members of SystemInformation to obtain system information and display it;

Class Class1

{

static void Main (string[] args)

{

Console.WriteLine ("Computer Name: _" +system.windows.forms.systeminformation.computername.tostring ());

Console.WriteLine ("Starting Mode: _" +system.windows.forms.systeminformation.bootmode.tostring ());

Console.WriteLine ("Domain name:

_ "+system.windows.forms.systeminformation.userdomainname.tostring" ());

}

}




The above code uses only three static members of the SystemInformation class, and other members ' properties and the information they can provide refer to MSDN. The results of the above code are shown in the following illustration:








Environment class



The second approach is through the Environment class, which provides information about the current environment and platform and how to manipulate them.



The environment class provides static methods in addition to some static members. We also illustrate the use of these methods and members by an example:



Class Class1

{

static void Main (string[] args)

{

Console.WriteLine ("System Logon Time: +environment.tickcount/1000/60+" minute.) ");

Console.WriteLine ("Current directory of User system:" +environment.currentdirectory);

Console.WriteLine ("Logical Disk letter used by the user system:" +environment.getlogicaldrives ());

Console.WriteLine ("System path of the user system: _" +environment.getfolderpath (Environment.SpecialFolder.System));

Console.ReadLine ();

}

}




Like the previous class, we used only a few members and methods of the environment class, other methods and members, and more information to refer to MSDN, and the above code executed the following results:








RegistryKey class



The third way is to query system information through the registry, which is the most traditional method and the most powerful method and the most complex method. The registry in. NET is encapsulated in the RegistryKey class, in the Microsoft.Win32 namespace.



This class is different from the first two classes, its public properties we can take advantage of very few opportunities, we have to obtain system information is mainly to use its OpenSubKey method, however, in order to use this method, we must have a fairly good understanding of the registry, which is the use of this class to obtain system Information complex. If you are familiar with the registry, you will be able to obtain very rich system information.



We are still using an example to illustrate the use of the RegistryKey class, in order to:



Class Class1

{

static void Main (string[] args)

{

RegistryKey rkey = registry.localmachine;

Rkey = Rkey.opensubkey ("hardware\\description\\system\\centralprocessor\\0");

Console.WriteLine ("Processor info:" +rkey.getvalue ("processornamestring"));

Rkey=registry.localmachine;

Rkey = Rkey.opensubkey ("Software\\microsoft\\windows _nt\\currentversion\\networkcards\\1");

Console.WriteLine ("Network card information:" + (String) rkey.getvalue ("Description"));

Console.ReadLine ();

}

}




The above example shows only processor information and network card information. If you look closely at the member information of the first two classes, you can find that it is not possible to get hardware information from the first two classes, and to obtain hardware information with the help of the Registry or WMI (Windows System Management method), but to use them, You have to have a general idea of them first. The above code results are as follows:






The above three methods only play a role in the hope of getting more information about the system will have to spend time to achieve, in addition to these three methods, there are other ways to obtain system information, such as through WMI.


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.