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 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.
 

 

From smart dummies

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.