Requirement description
Now there is a requirement:
I have two servers, A and B, where A is a video processing server and B is a data storage server. At this time, there is a video that needs to be processed in a series of processes on server A before uploading it to server B for storage.
In order to reduce the workload of manual processing, server A needs to actively send data to B, and then server B receives the data and stores it. However, in this process, I do not want server B to receive data uploaded by other ends other than server A, so when B receives data, it needs to verify the source of the data.
Here, when uploading data, server A carries the hardware information of its own server. By verifying the hardware information, it can determine whether the data source is server A.
Determine which platform the current program runs on
The IsOSPlatform method is used to indicate whether the current application is running on the specified platform:
public static bool IsOSPlatform ( osPlatform);
The parameter osPlatform represents an operating system platform:
value | Description |
---|---|
FreeBSD | Get an object representing the FreeBSD operating system |
Linux | Get an object representing the Linux operating system |
OSX | Get an object representing the OSX operating system |
Windows | Get an object representing the Windows operating system |
ManagementObjectSearcher class obtains hardware information
ManagementObjectSearcher is a constructor. Before using it, you need to introduce the namespace using System Management. This constructor receives a string as a parameter. This string is generally written as "select * from" + key. Here there are many values of key:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key); // Common Key Values// hardwareWin32_Processor, // CPU processorWin32_PhysicalMemory, // Physical memory stickWin32_Keyboard, // KeyboardWin32_PointingDevice, // Click the input device, including the mouse.Win32_FloppyDrive, // Floppy driveWin32_DiskDrive, // Hard driveWin32_CDROMDrive, // Optical driveWin32_BaseBoard, // MotherboardWin32_BIOS, // BIOS chipWin32_ParallelPort, // parallel portWin32_SerialPort, // Serial portWin32_SerialPortConfiguration, // Serial port configurationWin32_SoundDevice, // Multimedia settings, generally refer to sound cards.Win32_SystemSlot, // Motherboard slot (ISA & PCI & AGP)Win32_USBController, // USB controllerWin32_NetworkAdapter, // Network adapterWin32_NetworkAdapterConfiguration, // Network adapter settingsWin32_Printer, // printerWin32_PrinterConfiguration, // Printer settingsWin32_PrintJob, // Printer tasksWin32_TCPIPPrinterPort, // Printer portWin32_POTSModem, // MODEM Win32_POTSModemToSerialPort, // MODEM portWin32_DesktopMonitor, // MonitorWin32_DisplayConfiguration, // Graphics cardWin32_DisplayControllerConfiguration, // Graphics card settingsWin32_VideoController, // Graphics card details.Win32_VideoSettings, // Display mode supported by the graphics card. // operating systemWin32_TimeZone, // Time zoneWin32_SystemDriver, // DriverWin32_DiskPartition, // Disk partitionWin32_LogicalDisk, // Logical diskWin32_LogicalDiskToPartition, // The partition and location of the logical disk.Win32_LogicalMemoryConfiguration, // Logical memory configurationWin32_PageFile, // System page file informationWin32_PageFileSetting, // Page file settingsWin32_BootConfiguration, // System startup configurationWin32_ComputerSystem, // Brief computer informationWin32_OperatingSystem, // Operating system informationWin32_StartupCommand, // The system starts the program automaticallyWin32_Service, // System installation servicesWin32_Group, // System Management GroupWin32_GroupUser, // System group accountWin32_UserAccount, // User accountWin32_Process, // System processWin32_Thread, // System threadWin32_Share, // SharingWin32_NetworkClient, // Installed network clientWin32_NetworkProtocol, // Installed network protocol
Sample code
Get the CPU serial number
private string GetServerCode() { string code = ""; if (()) { ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Processor"); foreach (ManagementObject mo in ()) { code = mo["ProcessorId"].ToString().Trim(); break; } } else { code = ExecuteCommand("dmidecode -t Processor | grep ID | sort -u | awk -F':' '{print $2}'"); } ("serverCode:" + code); return (code ?? "").Replace(" ", "").Trim(); }
In the above example code for getting the CPU sequence number, a ManagementObjectSearcher object is first created, and then the ManagementObjectCollection collection is obtained through the Get() method, and then the ManagementObjectCollection is used to traverse the collection using the foreach method to get the ManagementObject data, and the desired attribute is obtained through managementObject[name] or (name).
However, before this, I didn't know what to fill in the value of this name. At this time, I just need to print it through console.
This is the article about using C# to obtain current device hardware information. For more related C# to obtain device hardware information, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!