'인포'에 해당되는 글 1건

  1. 2021.12.03 C# SystemInformation Class - 시스템 인포메이션 클래스
반응형

시스템 정보를 모두 확인해 보자.

 

폼에 리스트 박스와 텍스트 박스를 적절히 배치한다.

텍스트 박스 속성

Multiline = True

ScrollBars = Vertical

 

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
using System.Reflection;
 
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            Type t = typeof(System.Windows.Forms.SystemInformation);
            PropertyInfo[] pi = t.GetProperties();
            for (int i = 0; i < pi.Length; i++)
                listBox1.Items.Add(pi[i].Name);
 
            textBox1.Text = "The SystemInformation class has " + pi.Length.ToString() + " properties.\r\n";
 
            // 한 가지 프로퍼티만 필요하다면 아래와 같이 알아낼 수 있다.
            //string propertyName = "CaptionHeight";
            //PropertyInfo propertyValue = t.GetProperty(propertyName);
            //textBox1.Text = "The value of the " + propertyName + " property is: " + propertyValue.GetValue(null, null).ToString();
        }
 
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Return if no list item is selected.
            if (listBox1.SelectedIndex == -1return;
            // Get the property name from the list item.
            string propname = listBox1.Text;
 
            if (propname == "PowerStatus")
            {
                // Cycle and display the values of each property of the PowerStatus property.
                textBox1.Text += "\r\nThe value of the PowerStatus property is:";
                Type t = typeof(System.Windows.Forms.PowerStatus);
                PropertyInfo[] pi = t.GetProperties();
                for (int i = 0; i < pi.Length; i++)
                {
                    object propval = pi[i].GetValue(SystemInformation.PowerStatus, null);
                    textBox1.Text += "\r\n    PowerStatus." + pi[i].Name + " is: " + propval.ToString();
                }
            }
            else
            {
                // Display the value of the selected property of the SystemInformation type.
                Type t = typeof(System.Windows.Forms.SystemInformation);
                PropertyInfo[] pi = t.GetProperties();
                PropertyInfo prop = null;
                for (int i = 0; i < pi.Length; i++)
                    if (pi[i].Name == propname)
                    {
                        prop = pi[i];
                        break;
                    }
                object propval = prop.GetValue(nullnull);
                textBox1.Text += "\r\nThe value of the " + propname + " property is: " + propval.ToString();
            }
        }
    }
}
 

 

소스를 입력하고 빌드한다.

 

실행하면 위와 같은 화면이 나온다.

 

리스트 박스의 속성 아이템을 선택하면 텍스트 박스에 속성값이 표시된다.

 

 

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();            
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawString(System.Windows.Forms.SystemInformation.BorderSize.ToString(), Font, Brushes.Black, 1010);
            e.Graphics.DrawString(System.Windows.Forms.SystemInformation.CursorSize.ToString(), Font, Brushes.Black, 1030);
            e.Graphics.DrawString(System.Windows.Forms.SystemInformation.CaptionHeight.ToString(), Font, Brushes.Black, 1050);
        }
    }
}
 

 

위와 같이 간단히 몇 개만 조사할 수도 있다.

 

 

반응형
Posted by J-sean
: