C # Getting system information _ detailed exception information

Source: Internet
Author: User

From the user's point of view, when a program encounters an exception, you only need to give a simple prompt. but from the developer's point of view, very detailed information is required, which is useful for tracking bugs. exception-related details can be recorded in log files. in this way, it is easier for you to test and support users. If you encounter any problems, you can send the error log.

The exception handling statement is generally in the following format:

Try

{

// Some code

}

Catch (exception ex)

{

}

We usually get the prompt message ex. message, which briefly describes the exception, but ex also includes other detailed information.

Ex. GetType (). fullname. Detailed Exception name. We know that exception is general, and some more detailed exception names such as system. indexoutofrangeexception

Ex. Source returns the application name

Ex. targetsite returns the name of the function with an exception.

Ex. stacktrace has a lot of information, such as the name of the function with an error, the specific Cs source file, and the line in the source code.

 

In addition to this information, you may also want to know the version number, operating system of the program running, and hardware information. sometimes some bugs may only appear in an operating system. of course you can use other methods, but it is more convenient to collect the information by writing code.

System. reflection. Assembly. getexecutingassembly (). getname (). version. tostring (); // get the program version number

 

Obtaining computer software and hardware information

Using system. Management; // Add reference first, find system. mangement, and click OK.

 

String MSG = string. empty;

Managementobjectsearcher query = new managementobjectsearcher ("select * From win32_operatingsystem ");

Managementobjectcollection querycollection = query. Get ();

Foreach (managementbaseobject managementobject in querycollection)

{

MSG = managementobject ["caption"] + // operating system name, such as Microsoft Windows 7 Professional

Managementobject ["version"] + // system version, for example, 6.1.7601

Managementobject ["manufacturer"] + // operating system manufacturer, such as Microsoft Corporation

Managementobject ["csname"] + // computer name.

Managementobject ["windowsdirectory"]; // system installation directory, such as C: \ WINDOWS

}

Query = new managementobjectsearcher ("select * From win32_computersystem ");

Querycollection = query. Get ();

Foreach (managementobject in querycollection)

{

MSG + = managementobject ["manufacturer"] + // computer vendor, such as Dell Inc.

Managementobject ["model"] + // model, such

Managementobject ["systemtype"] + // CPU type, such as x64-based PC, indicating a 64-bit CPU

Managementobject ["totalphysicalmemory"]. tostring () + // memory size

Managementobject ["Domain"] + // Domain Name

Managementobject ["username"]; // user name when the computer is started

;

 

}

You can use managementobjectsearcher to view a lot of information related to computer software and hardware, such as hard disks, boards, and NICs.

 

 

 

 

 

 

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.