• 大小: 51KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-16
  • 语言: C/C++
  • 标签: C#  winform  USB  

资源简介

注意,这是C#的Demo,不是C++的。 是基于WMI监视USB插拔的原理做的Winform的Demo。 整个工程代码打包的,开发工具是VS2015

资源截图

代码片段和文件信息

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.Management;//注意了,这里不能直接复制粘贴的,做法:项目右键-添加引用-程序集,然后搜索System.Management,勾选添加,最后这里才能粘贴using System.Management

namespace CheckUSB_Demo
{
    public partial class Form1 : Form
    {
        USB ezUSB = new USB();
        public Form1()
        {
            InitializeComponent();
        }



        private void Form_Load(object sender EventArgs e)
        {
            ezUSB.AddUSBEventWatcher(USBEventHandler USBEventHandler new TimeSpan(0 0 3));
        }

        private void USBEventHandler(object sender EventArrivedEventArgs e)
        {
            //throw new NotImplementedException();

            if (e.NewEvent.ClassPath.ClassName == “__InstanceCreationEvent“)
            {
                this.SetText(“USB插入时间:“ + DateTime.Now + “\r\n“);
            }
            else if (e.NewEvent.ClassPath.ClassName == “__InstanceDeletionEvent“)
            {
                this.SetText(“USB拔出时间:“ + DateTime.Now + “\r\n“);
            }

            foreach (USBControllerDevice Device in USB.WhoUSBControllerDevice(e))
            {
                this.SetText(“\tAntecedent:“ + Device.Antecedent + “\r\n“);
                this.SetText(“\tDependent:“ + Device.Dependent + “\r\n“);
            }

        }

        private void Form_FormClosing(object sender FormClosingEventArgs e)
        {
            ezUSB.RemoveUSBEventWatcher();
        }





        // 对 Windows 窗体控件进行线程安全调用
        private void SetText(String text)
        {
            if (this.textBox1.InvokeRequired)
            {
                this.textBox1.BeginInvoke(new Action((msg) =>
                {
                    this.textBox1.AppendText(msg);
                }) text);
            }
            else
            {
                this.textBox1.AppendText(text);
            }
        }

    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     20480  2018-08-01 09:34  CheckUSB_Demo\.vs\CheckUSB_Demo\v14\.suo

     文件        189  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\App.config

     文件      10752  2018-08-01 10:05  CheckUSB_Demo\CheckUSB_Demo\bin\Debug\CheckUSB_Demo.exe

     文件        189  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\bin\Debug\CheckUSB_Demo.exe.config

     文件      26112  2018-08-01 10:05  CheckUSB_Demo\CheckUSB_Demo\bin\Debug\CheckUSB_Demo.pdb

     文件      22696  2018-08-01 10:07  CheckUSB_Demo\CheckUSB_Demo\bin\Debug\CheckUSB_Demo.vshost.exe

     文件        189  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\bin\Debug\CheckUSB_Demo.vshost.exe.config

     文件        490  2016-07-16 19:44  CheckUSB_Demo\CheckUSB_Demo\bin\Debug\CheckUSB_Demo.vshost.exe.manifest

     文件       4012  2018-08-01 10:05  CheckUSB_Demo\CheckUSB_Demo\CheckUSB_Demo.csproj

     文件       2259  2018-08-01 10:12  CheckUSB_Demo\CheckUSB_Demo\Form1.cs

     文件       2116  2018-08-01 10:04  CheckUSB_Demo\CheckUSB_Demo\Form1.Designer.cs

     文件       5817  2018-08-01 10:04  CheckUSB_Demo\CheckUSB_Demo\Form1.resx

     文件        897  2018-08-01 10:07  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\CheckUSB_Demo.csproj.FileListAbsolute.txt

     文件       1012  2018-08-01 10:05  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\CheckUSB_Demo.csproj.GenerateResource.Cache

     文件       2538  2018-08-01 10:05  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\CheckUSB_Demo.csprojResolveAssemblyReference.cache

     文件      10752  2018-08-01 10:05  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\CheckUSB_Demo.exe

     文件        180  2018-08-01 10:05  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\CheckUSB_Demo.Form1.resources

     文件      26112  2018-08-01 10:05  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\CheckUSB_Demo.pdb

     文件        180  2018-08-01 10:05  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\CheckUSB_Demo.Properties.Resources.resources

     文件        868  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7344  2018-08-01 09:57  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        525  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\Program.cs

     文件       1334  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\Properties\AssemblyInfo.cs

     文件       2839  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\Properties\Resources.Designer.cs

     文件       5612  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\Properties\Resources.resx

     文件       1100  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\Properties\Settings.Designer.cs

     文件        249  2018-08-01 09:34  CheckUSB_Demo\CheckUSB_Demo\Properties\Settings.settings

............此处省略16个文件信息

评论

共有 条评论