■ ManagementClass 클래스를 사용해 컴퓨터 타입을 구하는 방법을 보여준다.
▶ ComputerType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
namespace TestProject { /// <summary> /// 컴퓨터 타입 /// </summary> public enum ComputerType { /// <summary> /// Other /// </summary> Other = 1, /// <summary> /// Unknown /// </summary> Unknown, /// <summary> /// Desktop /// </summary> Desktop, /// <summary> /// LowProfileDesktop /// </summary> LowProfileDesktop, /// <summary> /// PizzaBox /// </summary> PizzaBox, /// <summary> /// MiniTower /// </summary> MiniTower, /// <summary> /// Tower /// </summary> Tower, /// <summary> /// Portable /// </summary> Portable, /// <summary> /// Laptop /// </summary> Laptop, /// <summary> /// Notebook /// </summary> Notebook, /// <summary> /// Handheld /// </summary> Handheld, /// <summary> /// DockingStation /// </summary> DockingStation, /// <summary> /// AllInOne /// </summary> AllInOne, /// <summary> /// SubNotebook /// </summary> SubNotebook, /// <summary> /// SpaceSaving /// </summary> SpaceSaving, /// <summary> /// LunchBox /// </summary> LunchBox, /// <summary> /// MainSystemChassis /// </summary> MainSystemChassis, /// <summary> /// ExpansionChassis /// </summary> ExpansionChassis, /// <summary> /// SubChassis /// </summary> SubChassis, /// <summary> /// BusExpansionChassis /// </summary> BusExpansionChassis, /// <summary> /// PeripheralChassis /// </summary> PeripheralChassis, /// <summary> /// StorageChassis /// </summary> StorageChassis, /// <summary> /// RackMountChassis /// </summary> RackMountChassis, /// <summary> /// SealedCasePC /// </summary> SealedCasePC } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
using System; using System.Management; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main(argumentArray) /// <summary> /// 프로그램 시작하기 /// </summary> /// <param name="argumentArray">인자 배열</param> private static void Main(string[] argumentArray) { Console.Title = "ManagementClass 클래스 : 컴퓨터 타입 구하기"; ComputerType computerType = GetComputerType(); Console.WriteLine($"컴퓨터 타입 : {computerType}"); } #endregion #region 컴퓨터 타입 구하기 - GetComputerType() /// <summary> /// 컴퓨터 타입 구하기 /// </summary> /// <returns>컴퓨터 타입</returns> private static ComputerType GetComputerType() { try { ManagementClass managementClass = new ManagementClass("Win32_SystemEnclosure"); foreach(ManagementObject managementObject in managementClass.GetInstances()) { foreach(int index in (ushort[])managementObject.GetPropertyValue("ChassisTypes")) { return (ComputerType)index; } } return ComputerType.Unknown; } catch { return ComputerType.Unknown; } } #endregion } } |