How to get computer information with C#?

Asked

Viewed 2,771 times

5

As information of the computer where the C#application is running, such as computer name, IP, status of the firewall, if you have antivirus installed, if antivirus is active, etc.

1 answer

9


The question is a little wide. 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 obtained easily. Some may need to be quite specific and/or unreliable. So in these cases probably not worth the effort.

One of the namespaces that provide this information is the System.Management. There are several classes 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 examples of use. 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 framework. Take a look at the System.Management.Instrumentation.

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

Some classes to start your study: ManagementObjectSearcher. ManagementObjectCollection.

If you like to wear LINQ has a library to access some of this information this way. I don’t know anything about the quality of it.

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

Something can still be obtained with the class SystemInformation.

Some information can be obtained through the Windows registry. Study the class Registry and other related classes. As I said, I would probably have to know what to look for to get relevant information without too 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 of 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"];
}

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.