cscの作法 その569

# 概要
cscの作法、調べてみた。
デバイスのUSB接続状況を監視するアプリ見つけたので、やってみた。

# 参考にしたページ

https://qiita.com/nakaziki/items/50e170ee895ea5d22c2e


# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/2cfc6386-7c84-41c5-9197-63344bc66a2f.png)

# サンプルコード

```

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Management;
using System.Threading.Tasks;
using System.Drawing;

namespace WindowsDeviceEventMonitor
{
	class USBDeviceInfo {
		public USBDeviceInfo(string deviceID) {
			this.DeviceID = deviceID;
		}
		public string DeviceID {
			get;
			private set;
		}
	}
	public partial class Form1: Form {
		private TextBox textBox_Msg;
		public const int WM_DEVICECHANGE = 0x00000219;
		List<USBDeviceInfo> usbDevicesBefore = new List<USBDeviceInfo>();
		int numBeforeDevices = 0;
		public Form1() {
			Text = "WindowsDeviceEventMonitor";
			Size = new Size(450, 450);
			textBox_Msg = new TextBox();
			textBox_Msg.Dock = DockStyle.Fill;
			textBox_Msg.Multiline = true;
			textBox_Msg.TabIndex = 2;
			textBox_Msg.ScrollBars = ScrollBars.Both;
			Controls.AddRange(new Control[] {
				textBox_Msg
			});
			Task.Run(() => CheckDevice());
		}
		private void CheckDevice() {
			var usbDevices = GetUSBDevices();
			string nowTime = DateTime.Now.ToString("HH:mm:ss.ff ");
			if (usbDevices.Count > numBeforeDevices)
			{
				foreach (var usbDevice in usbDevices)
				{
					bool bExistDevice = false;
					foreach (var usbDeviceBefore in usbDevicesBefore)
					{
						if (usbDevice.DeviceID == usbDeviceBefore.DeviceID)
						{
							bExistDevice = true;
							break;
						}
					}
					if (!bExistDevice)
					{
						string sTemp = string.Format("Add Device ID: {0}", usbDevice.DeviceID);
						AddMessage(nowTime + sTemp + Environment.NewLine);
					}
				}
			}
			else if (usbDevices.Count < numBeforeDevices)
			{
				foreach (var usbDeviceBefore in usbDevicesBefore)
				{
					bool bExistDevice = false;
					foreach (var usbDevice in usbDevices)
					{
						if (usbDevice.DeviceID == usbDeviceBefore.DeviceID)
						{
							bExistDevice = true;
							break;
						}
					}
					if (!bExistDevice)
					{
						string sTemp = string.Format("Del Device ID: {0}", usbDeviceBefore.DeviceID);
						AddMessage(nowTime + sTemp + Environment.NewLine);
					}
				}
			}
			AddMessage(Environment.NewLine);
			usbDevicesBefore = usbDevices;
			numBeforeDevices = usbDevices.Count;
		}
		static List<USBDeviceInfo> GetUSBDevices() {
			List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
			ManagementObjectCollection collection;
			using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
				collection = searcher.Get();
			foreach (var device in collection)
			{
				devices.Add(new USBDeviceInfo( (string)device.GetPropertyValue("DeviceID")));
			}
			collection.Dispose();
			return devices;
		}
		protected override void WndProc(ref Message m) {
			base.WndProc(ref m);
			switch (m.Msg)
			{
			case WM_DEVICECHANGE:
				Task.Run(() => CheckDevice());
			break;
			}
		}
		public void AddMessage(string str) {
			if (this.InvokeRequired)
				this.Invoke(new Action<string>(AddMessage), str);
			else
				this.textBox_Msg.AppendText(str);
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}





```

以上。