How to get information from the computer with C #?

5

As the information on the computer where the C # application is running, such as computer name, IP, firewall status, whether antivirus is installed, whether antivirus is enabled, etc.

    
asked by anonymous 23.03.2015 / 13:07

1 answer

9

The question is a little broad. I will show you the path that is most important. If you have specific questions you can ask specific new questions.

Keep in mind that not all information can be easily obtained. Some may need to be in a very specific and / or unreliable way. So in these cases probably not even worth the effort.

One of the namespaces that provide this information is the System.Management ". There are several classes there that provide much of the information you want. You have to search each of the classes and see which ones are useful. Most of them have use examples. All of this namespace is limited in Mono and will probably also be in .Net Core . So consider that it only works on Windows and the full implementation of the framework . Take a look at System.Management.Instrumentation .

Some classes provide the information as an SQL query, so you have to learn all the possibilities of queries . In the documentation goes the way to learn everything. This language is called WQL .

Some classes to begin your study: ManagementObjectSearcher . ManagementObjectCollection .

If you like to use LINQ has a library to access some of this information in this way. I do not know anything about the quality of it.

Another very useful class that provides other information is Environment . There is an example of use in the documentation. Most elements are obtained simply through properties. Some need to be enumerated.

Something can still be achieved with the class SystemInformation .

Some information can be obtained through the Windows registry. Searches the class Registry and other classes related issues. Like I said, you would probably have to know what to look for to get relevant information without much noise. It's easier when you want to know if something is present.

Some information can only be obtained with high permission.

An example code:

using System;
using System.Collections.Generic;
using System.Management;

var consulta = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
ManagementObjectCollection bios = consulta.Get();

foreach (ManagementObject obj in bios) {
    var item = new Win32_BIOS();
    item.BiosCharacteristics = (ushort[])obj["BiosCharacteristics"];
    item.BIOSVersion = (string[])obj["BIOSVersion"];
    item.BuildNumber = (string)obj["BuildNumber"];
    item.Caption = (string)obj["Caption"];
    item.CodeSet = (string)obj["CodeSet"];
    item.CurrentLanguage = (string)obj["CurrentLanguage"];
    item.Description = (string)obj["Description"];
    item.IdentificationCode = (string)obj["IdentificationCode"];
    item.InstallableLanguages = (ushort?)obj["InstallableLanguages"];
    item.InstallDate = (DateTime?)obj["InstallDate"];
    item.LanguageEdition = (string)obj["LanguageEdition"];
    item.ListOfLanguages = (string[])obj["ListOfLanguages"];
    item.Manufacturer = (string)obj["Manufacturer"];
    item.Name = (string)obj["Name"];
    item.OtherTargetOS = (string)obj["OtherTargetOS"];
    item.PrimaryBIOS = (bool?)obj["PrimaryBIOS"];
    item.ReleaseDate = (string)obj["ReleaseDate"];
    item.SerialNumber = (string)obj["SerialNumber"];
    item.SMBIOSBIOSVersion = (string)obj["SMBIOSBIOSVersion"];
    item.SMBIOSMajorVersion = (ushort?)obj["SMBIOSMajorVersion"];
    item.SMBIOSMinorVersion = (ushort?)obj["SMBIOSMinorVersion"];
    item.SMBIOSPresent = (bool?)obj["SMBIOSPresent"];
    item.SoftwareElementID = (string)obj["SoftwareElementID"];
    item.SoftwareElementState = (ushort?)obj["SoftwareElementState"];
    item.Status = (string)obj["Status"];
    item.TargetOperatingSystem = (ushort?)obj["TargetOperatingSystem"];
    item.Version = (string)obj["Version"];
}
    
23.03.2015 / 13:54