Exemplo n.º 1
0
 void DAlerter_DiskChangeEvent(DriveChangedArgs e)
 {
     //MessageBox.Show(e.DriveLetter + ", " + e.InterfaceType + ", " + e.ChangeType);
     try
     {
         if (this.InvokeRequired)
         {
             // this is what we do if the event comes in from another thread
             MethodInvoker del = delegate { DAlerter_DiskChangeEvent(e); };
             this.Invoke(del);
             return;
         }
         else
         {
             // this is what we do as a result of the invoke() or if its
             // on the same thread as the handler
             frmNotice.Show();
             Application.DoEvents();
             bDriveRescan     = true;
             cbDriveList.Text = "";
             cbDriveList.Items.Clear();
             tvFolderTree.Nodes.Clear();
             DataGridView1.Rows.Clear();
             GetDrives();
             bDriveRescan = false;
         }
     }
     catch (Exception de)
     {
         frmNotice.Hide();
         Application.DoEvents();
         MessageBox.Show("rescan error: " + de);
     }
     if (frmNotice != null)
     {
         frmNotice.Hide();
         Application.DoEvents();
     }
 }
Exemplo n.º 2
0
        /*
         * 0 = Unknown
         * 1 = No Root Directory
         * 2 = Removable Disk
         * 3 = Local Disk
         * 4 = Network Drive
         * 5 = Compact Disc
         * 6 = RAM Disk
         */

        private void Watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            DriveChangedArgs a1 = new DriveChangedArgs();

            /*
             * int iDriveType = 0;
             * ManagementBaseObject mbo, obj;
             * mbo = (ManagementBaseObject)e.NewEvent;
             * obj = (ManagementBaseObject)mbo["TargetInstance"];
             * try
             * { iDriveType = int.Parse(obj["DriveType"].ToString()); }
             * catch
             * { iDriveType = 0; }
             * DriveChangedArgs a1 = new DriveChangedArgs
             * {
             *      DriveLetter = obj["Name"].ToString()
             * };
             * if (obj.ClassPath.ToString().ToLower().Contains("mapped") || iDriveType == 4)
             *      a1.InterfaceType = "MappedDrive";
             * if (obj.ClassPath.ToString().Contains("Win32_DiskDrive") || iDriveType == 4)
             * {
             *      try
             *      {
             *              a1.InterfaceType = obj["InterfaceType"].ToString();
             *      }
             *      catch
             *      {
             *              a1.InterfaceType = "unknown";
             *      }
             *      try
             *      {
             *              a1.DriveLetter = GetDriveLetterFromDisk(obj["Name"].ToString());
             *              if (!USBdic.ContainsKey(obj["Name"].ToString()))
             *              { USBdic.Add(obj["Name"].ToString(), a1.DriveLetter); }
             *      }
             *      catch
             *      {
             *              if (USBdic.ContainsKey(obj["Name"].ToString()))
             *              {
             *                      a1.DriveLetter = USBdic[obj["Name"].ToString()];
             *                      USBdic.Remove(obj["Name"].ToString());
             *              }
             *              else
             *                      a1.DriveLetter = "unknown";
             *      }
             * }
             * switch (mbo.ClassPath.ClassName)
             * {
             *      case "__InstanceCreationEvent":
             *              a1.ChangeType = DriveChangeType.Create;
             *              break;
             *      case "__InstanceDeletionEvent":
             *              a1.ChangeType = DriveChangeType.Remove;
             *              break;
             *      case "__InstanceModificationEvent":
             *              a1.ChangeType = DriveChangeType.MediaChange;
             *              break;
             *      default:
             *              a1.ChangeType = DriveChangeType.Other;
             *              break;
             * }
             */

            try
            {
                DiskChangeEvent.Invoke(a1);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Watchereventarrived(): " + ex);
            }
        }