Use C # to get system information

zhaozj2021-02-16  52

The system information is not more external system software and hardware information, and information that needs to be detected is also this information. The .NET class library provides us with some of the functions required to obtain system messages, bringing a lot of convenience to our programming.

The following sections will indicate 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, which is included in the System.Windows.Forms name space. 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 there is no other way. The system information can be obtained by public static members of this class, and we can select the appropriate members as needed. Below we explain how to use these members with an example:

// SystemInformation class is in the System.Windows.Forms name space, you need to introduce the name space before using it;

Using system.windows.forms;

// Use the static member 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 Method: _" System.Windows.Forms.Systeminformation.bootmode.toString ());

Console.writeline ("Domain Name:

_ " System.windows.Forms.systeminformation.Userdomainname.tostring ());

}

}

The above code only uses three static members of the SystemInformation class, and the properties of other members and the information they can provide can be referred to MSDN. The execution result of the above code is shown in the figure below:

ENVIRONMENT class

The second method is to pass the Environment class, which provides information about the current environment and platforms and how they operate them.

In addition to some static members, the Environment category provides some static methods. We still use an example to illustrate these methods and members:

Class class1

{

Static void main (string [] args)

{

Console.writeline ("System Login Time: Environment.tickcount / 1000/60 " Minutes. ");

Console.writeline ("Current Directory of User System:" Environment.currentDirectory;

Console.writeline ("" "" "" "Dogical Capacity used by the user system:" Environment.getLogicalDrives ());

Console.writeline ("System Path of User System: _" Environment.GetFolderPath (Environment.SpecialFolder.system);

Console.readline ();

}

}

Like the previous class, we also only use several members and methods of Environment classes, other methods and members and more information can be referred to MSDN, the execution results of the above code are as follows:

RegistryKey class

The third way is to query system information by registry, which is the most traditional method, and is also the most powerful method and the most complex method. The registry in the .NET is encapsulated in the RegistryKey class, located in the Microsoft.Win32 namespace. This class is different from the first two classes. Its public attributes we can use very little. We mainly use its OpenSubkey method in order to obtain system information. However, in order to use this method, we must have a considerable understanding of the registry. OK, this is also the complexity of using this class to obtain system information. If you are familiar with the registry, you will get a very rich system information.

We still use an example to show the use of the RegistryKey class, in order to throw bricks:

Class class1

{

Static void main (string [] args)

{

RegistryKey rkey = registry.localmachine;

Rkey = rkey.opensubkey ("Hardware // Description // System // Centralprocessor // 0");

Console.writeline ("Processor Information:" Rkey.getValue ("ProcessornameString"));

Rkey = registry.localmachine;

RKEY = rkey.opensubkey ("Software // Microsoft // Windows _NT // CurrentVersion // NetworkCards // 1");

Console.writeline ("NIC Information:" (String) Rkey.GetValue ("Description"));

Console.readline ();

}

}

The above example only shows processor information and network card information. If you look at the member information of the first two classes, you can find that you want to get hardware information from the previous class, you have to get hardware information, you have to use the registry or It is WMI (window system management method), but you must use them, you must first have a general understanding of them. The above code results are as follows:

The above three methods are only the role of throwing bricks, and I hope that more system information has to spend itself; in addition to these three methods, system information can also be obtained by other methods, such as by WMI.

转载请注明原文地址:https://www.9cbs.com/read-23430.html

New Post(0)