0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その569

Last updated at Posted at 2025-03-10

概要

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

参考にしたページ

写真

image.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());
		}
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?