让你的计算机只识别你的优盘

     
     目前各种优盘病毒横行,特别是我所在实验室的电脑是会被别人使用的,对别人优盘上面的病毒十分烦恼,有一段时间我在CIMS参数里面封掉了USB接口,然而启动USB接口还需要重新启动计算机,很麻烦。
     那么如何用程序的方式关闭和启动USB接口呢?如何让你的计算机只使用你的优盘呢?
     如果感兴趣,继续看下去。
     1.在form1类里面重载windows消息接受函数,检测优盘插入。 
     2.检测优盘的认证信息。    
     3.如果认证不是你的优盘,掉用windows外壳组件shell.dll。执行优盘的右击菜单中的“弹出”。
     注:1.可以在你的优盘中放一个文本文件(txt,excel、ini、rtf、xml等)存储密码信息,该密码作为认证的凭证。
         2.通过shell.dll调用能执行windows系统中的几乎所有的快捷菜单(前面有文章已经给出了源码),当然执行优盘格式化也是可以的,如果防止病毒传染,检测到优盘插入,直接格式化也不错。

   主要源码:
         1.检验U盘插入代码
         const int DBT_DEVICEARRIVAL = 0x8000;
         const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
         protected override void WndProc(ref Message mess)
          {
            try
            {
                if (m.Msg == WM_DEVICECHANGE)
                {
                    switch (m.WParam.ToInt32())
                    {
                      case DBT_DEVICEARRIVAL://U盘插入
                            DriveInfo[] s = DriveInfo.GetDrives();
                            foreach (DriveInfo drive in s)
                            {
                                if ((drive.DriveType == DriveType.Removable)
                                                &&(drive.Name.ToString()=="A:\\"))
                                {
                                    //listBox1.Items.Add("U盘已插入,盘符为:"            
                                                             +drive.Name.ToString());
                                    //MessageBox.Show("优盘插入!盘符为:" + drive.Name.ToString
                                          (),"注意");
                                    tan_chu();
                                    break;
                                }
                            }
                            break;
                        case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
                            //MessageBox.Show("优盘已经卸载。");
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.WndProc(ref mess);
        }

        2.弹出U盘代码

        static private void tan_chu()
        {
            Shell32.Shell sh = new Shell32.Shell();

            Shell32.Folder sf = sh.NameSpace(17);//diann
            Shell32.FolderItems fis = sf.Items();

            while (true)
            {
                try
                {
                    for (int i = 0; i < fis.Count; i++)
                    {
                        string itemName = fis.Item(i).Name.ToString();
                        if (itemName == "可移动磁盘 (K:)")
                        {
                            Shell32.FolderItemVerbs fivs = fis.Item(i).Verbs();

                            foreach (Shell32.FolderItemVerb fv in fivs)
                            {
                                string name = fv.Name.ToString();
                                if (name == "弹出(&J)")
                                {
                                    fv.DoIt();
                                    // break;
                                }
                                //if (name == "扫描病毒(&K)")
                                //{
                                //    fv.DoIt();
                                //    // break;
                                //}
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string exx = ex.Message.ToString();
                }
            }

你可能感兴趣的:(计算机)